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

  • @Autowire not working with ContextLoaderListener on Tomcat

    Hi,

    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.

    web.xml
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
       <display-name>XYZ-Server</display-name>
       <welcome-file-list>
          <welcome-file>index.html</welcome-file>
       </welcome-file-list>
       <listener>
          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
       </listener>
       <context-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:applicationContext.xml</param-value>
       </context-param>
       <servlet>
          <servlet-name>xyz</servlet-name>
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
          <load-on-startup>1</load-on-startup>
       </servlet>
       <servlet-mapping>
          <servlet-name>xyz</servlet-name>
          <url-pattern>*.rpc</url-pattern>
       </servlet-mapping>
    </web-app>

    xyz-servlet.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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
          http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
          http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
    
    	<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="de.foo.bar.XyzService" />
    	</bean>
    
    </beans>
    Does any1 know whats going wrong here?

    TIA byto

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

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

    Comment


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

      Comment


      • #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?

        Comment


        • #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.

          Comment


          • #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.

            Comment


            • #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...

              Comment

              Working...
              X