@autowired annotation issue, not bean injection in class using Spring3.0 hibernation

following my class:

package com.abc.trade.util; public class StockTraderLogger { static Logger logger = Logger.getLogger("StockTraderLogger"); @Autowired ConfigService configService; public static void debug(Object className, Object logMessage) { try { System.out.println("in debug.. "); StockTraderLogger stl =new StockTraderLogger(); stl.addMessage(""+convertToString(className)+"\t"+convertToString(logMessage)); System.out.println("in debug..post "); } catch (DataAccessException e) { System.out.println("Caught exception..."); e.printStackTrace(); } } public void addMessage(String message) throws DataAccessException { System.out.println("in add message of util. "); System.out.println("String: " + configService); configService.addMessage(message); } } 

@Autowire annotation does not work. It displays the configService value as null when the addMessage method is addMessage . however, it is correctly injected into some of my Controller classes, but not here.

Can someone explain what the problem is? and how to solve this problem?

Code for XML: (beansdefinition.xml)

  <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:component-scan base-package="com.abc.trade.util"/> <context:component-scan base-package="com.abc.trade.service"/> <!-- Hibernate Configuration --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="annotatedClasses"> <list> <value>com.abc.trade.model.Order</value> <value>com.abc.trade.model.Profile</value> <value>com.abc.trade.model.Log</value> </list> </property> </bean> <tx:annotation-driven/> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <bean id="commonService" class="com.abc.trade.framework.service.CommonServiceImplementor"> <property name="commonDao" ref="commonDao"/> </bean> <bean id="commonDao" class="com.abc.trade.framework.dao.HibernateDAO"> <property name="sessionFactory"><ref local="sessionFactory"/></property> </bean> <bean id="configService" class="com.abc.trade.service.ConfigServiceImplementor" parent="commonService"> </bean> <import resource="../context/springws-servlet.xml"/> </beans> 

Other XML is: (Springmvc-servlet.xml)

 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:webflow="http://www.springframework.org/schema/webflow-config" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/webflow-config http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.0.xsd"> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/jsp/"/> <property name="suffix" value=".jsp"/> </bean> <context:component-scan base-package="com.abc.trade.controller" /> <context:component-scan base-package="com.abc.trade.util"/> <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basename" value="messages" /> </bean> <!-- Exception Resolver --> <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="exceptionMappings"> <props> <prop key="com.abc.trade.framework.exception.DataAccessException"> errorPage</prop> <prop key="java.sql.SQLException">errorPage</prop> <prop key="java.lang.Exception">errorPage</prop> </props> </property> </bean> </beans> 

Thanks in advance.

configService :

 package com.abc.trade.service; import org.springframework.stereotype.Service; import com.abc.trade.framework.exception.DataAccessException; public interface ConfigService { public void addMessage(String message) throws DataAccessException; } 

Configuration Service Developer:

 package com.abc.trade.service; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.abc.trade.framework.exception.DataAccessException; import com.abc.trade.framework.service.CommonServiceImplementor; import com.abc.trade.model.Log; import com.abc.trade.model.Mode; import com.abc.trade.util.StockTraderLogger; @Service("configService") public class ConfigServiceImplementor extends CommonServiceImplementor implements ConfigService{ String errorMessage = ""; @Override public void addMessage(String message) { System.out.println("in add message of service..........."); Log log = new Log(); try{ log.setMessage(message); System.out.println("Message is: "+message); int i=save(log); }catch(Exception e) { errorMessage = "Error in saving debug message"; e.printStackTrace(); //throw new DataAccessException(errorMessage); } } } 
+6
source share
4 answers

StockTraderLogger is not declared as a spring bean and does not exist in the context of spring, and for this reason the injection will not work.

 <bean id="StockTraderLogger" class="com.abc.trade.util.StockTraderLogger"/> 

or

 @Component public class StockTraderLogger { /**/ } 
+8
source

Here the problem is the debugging method:

  StockTraderLogger stl =new StockTraderLogger(); 

This is not spring management. You can introduce a spring-driven bean into an unmanaged one in two ways. Here you can enter configService in StockTraderLogger as:

1) By AutowireCapableBeanFactory:

  ApplicationContext ctx = new ClassPathXmlApplicationContext("beansdefinition.xml"); StockTraderLogger stl = new StockTraderLogger(); ctx.getAutowireCapableBeanFactory().autowireBeanProperties(stl, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true); 

2) Using spring AOP @Configurable annotation, which marks the class as acceptable for Spring-Driven configuration (for example, objects created by an instance of a "new" statement).

  @Configurable public class StockTraderLogger { ... } and specifying this <context:spring-configured/> in beansdefinition.xml. 

More information about this spring aop way here .

+3
source

Add this to applicationContext.xml :

xmlns:mvc="http://www.springframework.org/schema/mvc"

and

http://www.springframework.org/schema/mvc to xsi:schemalocation

The mvc annotation required for annotated controllers and other functions:

<mvc:annotation-driven />

+2
source

I think you are missing

 <context:annotation-config /> 

Also make sure your ConfigService class has

 @Service("configService") 

Annotations, this will make this candidate a class for auto-preparation.

and you should use

 <context:component-scan base-package="package" /> 

for the package name ConfigService.

+1
source

Source: https://habr.com/ru/post/887721/


All Articles