Announcement Announcement Module
No announcement yet.
@Autowire not working with ContextLoaderListener on Tomcat Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • @Autowire not working with ContextLoaderListener on Tomcat


    Ive autowired my services and daos. It works fine while running the unit tests. But when Im deploying on the tomcat, @Autowired will be ignored (properties are null at runtime). When switch from @Autowired to "manual" configuration, it works fine.

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app id="WebApp_ID" version="2.4" xmlns="" xmlns:xsi="" xsi:schemaLocation="">

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="" xmlns:xsi="" xmlns:aop="" xmlns:context="" xmlns:p="" xmlns:tx="" xmlns:util=""

    	<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
    	<bean name="/xyzService.rpc" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
    		<property name="service" ref="xyzService" />
    		<property name="serviceInterface" value="" />
    Does any1 know whats going wrong here?

    TIA byto

  • #2
    1) Tell the container to use annotations.

    <context:annotation-config />
    2) Read documentation


    • #3
      Urgs. I have to be blind. Thanks!


      • #4
        Hello Marten, sorry for resurrect this topic, but I need to ask something (more or less) similar

        I'm studying for Spring certification and I got a problem. I'm making my examples and testing they in TestNG. Thats it, I test, test, test and OK. Using autowiring, AOP, AspectJ proxies and using both instrumentation classes of Spring and AspectJ.

        So I've see something strange. When I use autowiring in my local tests, it works well but I don't need to declare a <context:annotation-config />. When I was almost finishing my studies (and focusing on SpringMVC), in web environment autowiring didn't work, only if I put a <context:annotation-config /> in applicationContext.xml file (but not in springapp-servlet.xml)

        My question is: If I use all these stuffs like instrumentations(weaving) it can influence my way to defining my beans? In other words, it is the main reason for autowiring works in my test environment without declaration of annotation-config?


        • #5
          No in the test there is already automatic wiring going on, you will need to tell the container to do something with the Annotations else it doesn't work.


          • #6
            I have exactly the same problem - it works in JUnits but not in the application. Clearly, so many people have the same issue that this is a MASSIVE DESIGN FLAW. This should just work out of the box.


            • #7
              No it shouldn't... If you understand the ApplicationContext and the role Bean(Factory)PostProcessors (and understand annotations) it isn't really strange to tell the context to do something with annotations. The problem with people is that they don't read the manual not even a quickstart...