Announcement Announcement Module
Collapse
No announcement yet.
Autowiring of beans in parent context not working Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Autowiring of beans in parent context not working

    Hello guys,
    I'm trying to deploy two web applications on Tomcat 6, which have common parent context, here is scenario:
    1) My parent context is under tomcat_home/share/lib/springParent.jar
    2) springParent.jar contains classes:
    - SampleService:
    Code:
    @Service
    public class SampleServiceImpl implements SampleService{
    
        @Autowired
        @Qualifier("testDao")
        private TestDao dao;
        
        public List<Object> getSomething() {
    	
    	return dao.getSomething();
        }
    }
    - TestDao
    Code:
    public class TestDaoImpl extends BaseDao  implements TestDao {
         public List<Object> getSomething() {
              return null;
          }
    - beanRefContext.xml
    Code:
    <?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: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/context http://www.springframework.org/schema/context/spring-context.xsd">
    
    	<context:spring-configured />
    	<context:component-scan base-package="test" />
    	
        <bean id="parent.context" class="org.springframework.context.support.ClassPathXmlApplicationContext">
            <constructor-arg>
                <list>
                    <value>services-context.xml</value>
                </list>
            </constructor-arg>
        </bean>
    
    </beans>
    -services-context.xml
    Code:
    <?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: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/context http://www.springframework.org/schema/context/spring-context.xsd" default-autowire="byType">
    
    	<context:component-scan base-package="test" />
    
    	<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
    	<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
    
    	<import resource="applicationContext-db.xml" />
    	
    	<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
      		<property name="locations">
    	   		<list>
    	   			<value>test.properties</value>
    	    	</list>
    	  	</property>	
    	</bean>
    </beans>
    - applicationContext-db.xml
    Code:
    <?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:jee="http://www.springframework.org/schema/jee"
    	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/jee
           http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
           http://www.springframework.org/schema/tx 
           http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd"
    	default-lazy-init="false">
    
    	<context:annotation-config />
    
    	<!-- ********************************************* -->
    	<!-- **************** DB CONFIGURATION *********** -->
    	<!-- ********************************************* -->
    
    	<bean id="dataSource">
    	<bean id="sessionFactory">
    	<bean id="transactionManager">
    	<tx:annotation-driven transaction-manager="transactionManager" />
    
    	<!-- ********************************************* -->
    	<!-- ********************* DAO'S ***************** -->
    	<!-- ********************************************* -->
    
    	<bean id="baseDao" class="test.BaseDao" abstract="true">
    		<property name="sessionFactory" ref="sessionFactory" />
    	</bean>
    	<bean id="testDao" class="test.TestDaoImpl"
    		parent="baseDao" />
    
    </beans>
    2) In my client application I have services, which use SmpleService from springParent.jar e.g.:
    Code:
    public class ClientServiceImpl implements ClientService {
    
        @Autowired
        private test.SampleService ss;
        public List<Object> getSomething() {
    	
    	return ss.getSomething();
        }
    Applications start without errors and it seems like all beans were detected by spring:
    Code:
    21:35:17,642 INFO  [ClassPathXmlApplicationContext:456] Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@396ac3: startup date [Tue Jul 12 21:
    35:17 GMT 2011]; root of context hierarchy
    21:35:17,646 INFO  [XmlBeanDefinitionReader:315] Loading XML bean definitions from class path resource [services-context.xml]
    21:35:17,676 INFO  [XmlBeanDefinitionReader:315] Loading XML bean definitions from class path resource [applicationContext-db.xml]
    21:35:17,809 INFO  [PropertyPlaceholderConfigurer:177] Loading properties file from class path resource [test.properties]
    21:35:17,890 INFO  [DefaultListableBeanFactory:555] Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@a97a9c: defining
    beans [sampleServiceImpl,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotatio
    nProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springf
    ramework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor#0,org.springframework.context.annotation.CommonAnnotationBeanPostProcessor#0,dataSource,sessionFact
    ory,transactionManager,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor#0,baseDao,testDao,propertyPlaceholderConfigurer]; root of factory hierarchy
    21:35:18,035 INFO  [DriverManagerDataSource:153] Loaded JDBC driver: org.postgresql.Driver
    21:35:18,186 INFO  [AnnotationSessionFactoryBean:743] Building new Hibernate SessionFactory
    21:36:01,686 INFO  [HibernateTransactionManager:383] Using DataSource [[email protected]e0] of Hibernate SessionFactory fo
    r HibernateTransactionManager
    21:36:01,785 INFO  [XmlWebApplicationContext:456] Refreshing Root WebApplicationContext: startup date [Tue Jul 12 21:36:01 GMT 2011]; parent: ApplicationContext 'parent.context'
    21:36:01,789 INFO  [XmlBeanDefinitionReader:315] Loading XML bean definitions from ServletContext resource [/WEB-INF/applicationContext.xml]
    21:36:01,846 INFO  [DefaultListableBeanFactory:555] Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@146283c: defining
     beans [org.springframework.context.config.internalBeanConfigurerAspect,simpleServiceImpl,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,
    org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework
    .context.annotation.internalCommonAnnotationProcessor,org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor#0,org.springframework.context.anno
    tation.CommonAnnotationBeanPostProcessor#0]; parent: org.springframework.beans.factory.support.DefaultListableBeanFactory@a97a9c
    21:36:01,856 INFO  [ContextLoader:214] Root WebApplicationContext: initialization completed in 45441 ms
    SampleService is correctly initialized on the client side, and ClientService can invoke
    Code:
    ss.getSomething()
    method without errors.
    The problem is that dao property in SampleService is null and Nullpointer is thrown.
    It looks like autowiring not working for beans in parent context.
    Did I missed something in configuration of parent context and what I have to change to enable autowiring in SampleService ?
    Last edited by bebop; Jul 12th, 2011, 06:52 PM.

  • #2
    Hello

    I am confused

    1) springParent.jar is the parent and you are using

    Code:
    @Service
    public class SampleServiceImpl implements SampleService{
    
        @Autowired
        @Qualifier("testDao")
        private TestDao dao;
    Question, if this is called from none 'child' project, I mean by the server itself, like a simple application, it work? Totally sure it work?

    SampleService is correctly initialized on the client side, and ClientService can invoke ss.getSomething() method without errors.
    OK, the call is invoked from the client side and work without problems

    Therefore my confusion is

    The problem is that dao property in SampleService is null and Nullpointer is thrown.
    when happen this?

    BTW


    Code:
    	
    <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
    <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
    it is old, you could consider the <context:annotation-config />

    Post the complete error stack trace and the XML configuration of the client side where are acceding to the Parent scope (you only included the ClientServiceImpl class)

    About
    1) My parent context is under tomcat_home/share/lib/springParent.jar
    Did you tested moving this jar file within your WEB-INF/lib client application?

    Comment


    • #3
      Hello dr_pompeii,
      thanks for response. I didn't make myself clear. Here are details:
      1) I have parent application context in springParent.jar
      2) In my web applications I have child application contexts, which inherit from parent.
      3) By saying
      SampleService is correctly initialized on the client side
      I meant that SampleService is correctly injected (through @Autowired annotation) to services, which are defined in child application contexts of web applications.
      4) Problem appeared during invocation of SampleService.getSomething() method because dao reference was null.
      Code:
      @Service
      public class SampleServiceImpl implements SampleService{
      
          @Autowired
          @Qualifier("testDao")
           private TestDao dao;  ← Null reference – autowired not working, although testDao bean was registered in parent context,
                                                                       the reference was not injected to SampleService.
          
          public List<Object> getSomething() {
              
              return dao.getSomething(); ← null pointer exception because dao is null 
          }
      }
      I have already solved the problem and I forgot to update the post. In my case the problem was that I had duplicated libs of Spring in lib directory of WAR files and share directory on tomcat. When I removed Spring libs from WAR files everything works fine.
      Thanks for the tip with <context:annotation-config /> I will check it.
      Last edited by bebop; Jul 17th, 2011, 07:50 AM.

      Comment

      Working...
      X