Announcement Announcement Module
Collapse
No announcement yet.
@Component, @Autowired and xml mixed configuration not working Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @Component, @Autowired and xml mixed configuration not working

    Hello guys. I tried everything but I'm at a loss here. I have used this approach before when working in a project from scratch, but now I'm working on a legacy system which already uses Spring. I'm trying to create a bean with @Component and have it autowired with beans defined in one of the application contenxt xmls.

    This is what I have in web.xml
    Code:
    ...
    <context-param>
    		<param-name>contextConfigLocation</param-name>
    		<param-value>classpath*:spring/applicationContext-*.xml</param-value>
    	</context-param>
    ...
    Inside the "spring" directory I have several xml files. I'm interested in two of them:

    applicationContext-dao.xml
    applicationContext-service.xml

    In applicationContext-dao.xml I have 2 daos: "permissionDao" and "utilitiesDao". I can find them and wire them through xml no problem, including wiring them into beans defined in other xml files.

    Now, in applicationContext-service.xml, I added the following:
    <context:annotation-config />
    <context:component-scan base-package="package.where.services.reside"/>

    I can access any services annotated with @Component without any problems through appContext.getBean(), which would indicates the component-scan tag is working as expected. However, services created that way do not get their dependencies injected (I tried @Autowired, @Qualifier, @Resource and all possible permutations with fields and setters).

    Is there something I'm missing? I also tried importing the dao context xml into the services xml but still it didn't work.

    P.S. I'm using version 2.5.1

    P.S. I just created a minimal project and did the same configuration as the legacy project and it just worked. I loaded the xmls using ClassPathXmlApplicationContext.
    Two classes and two xmls:
    Code:
    @Component("classA")
    public class ClassA {
        @Resource(name = "classB")
        private ClassB classB;
    
        public ClassB getClassB() {
            return classB;
        }
    }
    
    public class ClassB {
    
    }
    
    ===appContext1.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:util="http://www.springframework.org/schema/util"
           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/util
    	                       http://www.springframework.org/schema/util/spring-util-2.5.xsd
    	                       http://www.springframework.org/schema/context
    	                       http://www.springframework.org/schema/context/spring-context-2.5.xsd">
      <context:component-scan base-package="test"/>
    </beans>
    
    
    ===appContext2.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:util="http://www.springframework.org/schema/util"
           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/util
    	                       http://www.springframework.org/schema/util/spring-util-2.5.xsd
    	                       http://www.springframework.org/schema/context
    	                       http://www.springframework.org/schema/context/spring-context-2.5.xsd">
      <bean id="classB" class="test.ClassB"/>
    </beans>
    Last edited by daniel.mfreitas; Oct 26th, 2010, 04:36 PM.

  • #2
    I have found why the properties are not set. There's a BeanFactoryPostProcessor that also implements ApplicationContextAware and uses the application context to load the bean to do some dynamic resgistration (completely unrelated to Spring). It seems that calling applicationContext.getBean("...") inside a BeanFactoryPostProcessor stops the component from being fully initialized.

    If I remove the @Component annotation and define the bean through xml, it all works fine. Why only component-scan would behave like that I don't know. Maybe it's related to the order in which stuff happens internally.

    I wonder if this also happens in Spring 3.X.

    Anyways, I will try to move this code to a point in time where all spring beans are fully initialized. Is there a FullyInitializedSpringContextAndBeans interface or something like that? If not I guess I will have to write some startup code outside of spring and call it though other means.

    Comment

    Working...
    X