Announcement Announcement Module
Collapse
No announcement yet.
@Autowired not inject dependencies Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @Autowired not inject dependencies

    Hi everyone.

    I have some problem with the @Autowired annotation: the object injected is always null.. it looks like Spring is ignoring the annotation.


    Let me explain the structure of my project.

    I have a "kernel-project" where I have coded my model and the DAOs. From the Test classes of "kernel-project" I can inject the DAOs without problem.

    Take for example one DAO class. This is annotated with

    Code:
    @Repository("verbaleMtBTTDao")
    then in my context file I have defined

    Code:
    <bean class="it.cpmapave.mt.verbali.dao.jpa.VerbaleMtBTTDaoJpa" id="verbaleMtBTTDao"/>
    I have also put:
    Code:
    <context:annotation-config/>
    <context:component-scan base-package="it.cpmapave.mt.verbali"/>

    Then I have a "main-project" where I include as a maven dependencies the "kernel-project". The "main-project" is Spring Remote server side.

    I have injected a DAO in the Remote interface implementation. And here the DAO object remain null..

    I have annotated this class with @Component annotation and I have declared the field

    Code:
    @Autowired
    VerbaleMtBTTDao verbaleMtBTTDao;
    I have also tried to annotate the field with @Resource annotation, but nothing change..


    In web.xml file of "main-project", I have defined the context configuration file

    Code:
    <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
            	classpath*:META-INF/spring/spring-master.xml
            	classpath*:META-INF/spring/spring-security.xml	
            	classpath*:META-INF/spring/spring-kernel.xml
            </param-value>
    </context-param>
    where spring-master.xml and spring-security.xml are defined in the "main-project" and spring-kernel.xml is defined in "kernel-project".

    I have also tried to not put spring-kernel.xml in the contextConfigLocation definition, but i try to import spring-kernel.xml as a resource in spring-master.xml, thinking that may be something change.. but the situation still remain the same.

    In my spring-master.xml I have also tried to add the tags:

    Code:
    <context:annotation-config/>
    <context:component-scan base-package="it.cpmapave.mt.verbali"/>
    But I think it is not necessary because of they are already defined in the "kernel-project" imported context.

    Someone have any ideas?

    Thank you for any reply!!!
    Marco

  • #2
    Why are you using component-scanning AND defining your beans in xml... Use either one not both...

    Are you using the spring constructed components? Make sure you aren't creating new instances yourself. Do you only have a ContextLoaderListener or do you also have a (Message)DispatcherServlet?

    Comment


    • #3
      Thank you for your reply!

      Originally posted by Marten Deinum View Post
      Why are you using component-scanning AND defining your beans in xml... Use either one not both...
      Ok. I have removed DAOs beans definition from xml!

      Originally posted by Marten Deinum View Post
      Are you using the spring constructed components? Make sure you aren't creating new instances yourself.
      I'm not sure to have understood your question.. but I'm not creating new instance myself..

      Originally posted by Marten Deinum View Post
      Do you only have a ContextLoaderListener or do you also have a (Message)DispatcherServlet?
      I also have a DispatcherServlet

      Code:
      <servlet>
      	<servlet-name>remoting</servlet-name>
              <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
              <init-param>
                  <param-name>contextConfigLocation</param-name>
                  <param-value>/WEB-INF/spring/remoting-servlet.xml</param-value>
              </init-param>
            	<load-on-startup>1</load-on-startup>
          </servlet>

      Despite the change of bean definition in xml.. nothing has changed!

      Comment


      • #4
        Can you please post your full configuration (including web.xml). You could simply zip it and attach it. (It probably is to verbose to post normally).

        Comment


        • #5
          Originally posted by Marten Deinum View Post
          Can you please post your full configuration (including web.xml). You could simply zip it and attach it. (It probably is to verbose to post normally).
          web.xml
          Code:
          <?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
          <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
          
              
              <display-name>remote verbali</display-name>
              
              <description>Remote verbali application</description>
            
            	 <context-param>
                  <param-name>contextConfigLocation</param-name>
                  <param-value>
                  	classpath*:META-INF/spring/spring-master.xml
                  	classpath*:META-INF/spring/spring-security.xml	
                  </param-value>
              </context-param>
          
          	<filter>
            		<filter-name>springSecurityFilterChain</filter-name>
            		<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
          	</filter>
           
          	<filter-mapping>
            		<filter-name>springSecurityFilterChain</filter-name>
            		<url-pattern>/*</url-pattern>
          	</filter-mapping>
          	
              <servlet>
          		<servlet-name>remoting</servlet-name>
                  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
                  <init-param>
                      <param-name>contextConfigLocation</param-name>
                      <param-value>/WEB-INF/spring/remoting-servlet.xml</param-value>
                  </init-param>
                	<load-on-startup>1</load-on-startup>
              </servlet>
          
           	<listener>
                  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
              </listener>
          	
             <servlet-mapping>
                  <servlet-name>remoting</servlet-name>
                  <url-pattern>*.service</url-pattern>
              </servlet-mapping>
              
              <session-config>
                  <session-timeout>30</session-timeout>
              </session-config>
          </web-app>

          remoting-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:context="http://www.springframework.org/schema/context"
                 xmlns:tx="http://www.springframework.org/schema/tx"
                 xmlns:p="http://www.springframework.org/schema/p"
                 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-2.5.xsd
                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
           
              <bean id="remoteService" class="it.cpmapave.mt.verbali.remote.service.RemoteServiceImpl"/>
          	
              <bean id="remoteHttpInvoker" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
                  <property name="service">
                      <ref bean="remoteService"/>
                  </property>
                  <property name="serviceInterface">
                      <value>it.cpmapave.mt.verbali.remote.service.RemoteService</value>
                  </property>
              </bean>
              
              <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
                  <property name="mappings">
                      <props>
                          <prop key="/remoteService.service">remoteHttpInvoker</prop>
                      </props>
                  </property>
              </bean>   
          </beans>

          Comment


          • #6
            Found the mistake!

            I was importing .xml of the included jar package from a configuration file defined in contextConfigLocation.

            Importing instead from org.springframework.web.servlet.DispatcherServlet configuration file works!

            Thank you

            Comment


            • #7
              You shouldn't import it, it will duplicate your beans. You need to add context:annotation-config to the DispatcherServlets configuration, that way @Autowired will start to work. DispatcherServlet uses the ContextLoaderListener (at least the ApplicationContext) as its parent, it can access beans from the parent, that is where it all ends. There is no proxying, or inheritance of the Bean(Factory)PostProcessors.

              Comment

              Working...
              X