Spring autowire works, then doesn't

My spring autowiring seemed to be failing, so I made a postconstruct method to print out what was happening. Turns out my service impl class is being instantiated twice, and the second time the autowiring is failing.

Here's the console output.

Which is generated by this part of my impl class

@Service
public class DataMgmtWebServiceImpl implements DataMgmtPortType {

    @Autowired
    private CCRQueueDAO ccrQueueDAO;
    @Autowired
    private RSNCodeDAO rsnCodeDAO;

    private FileManagement fileManagement;

    private boolean testing = false;

    public DataMgmtWebServiceImpl() {
            System.out.println("default " + ccrQueueDAO);
    }


    @PostConstruct
    public void init() {
            System.out.println("init: " + ccrQueueDAO);
            SOAPToDatabase.rsnCodeDAO = rsnCodeDAO;
    }
    ...
}

ApplicationContext.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:context="http://www.springframework.org/schema/context"
    xmlns:jaxws="http://cxf.apache.org/jaxws"
    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-3.0.xsd
    http://cxf.apache.org/jaxws
    http://cxf.apache.org/schemas/jaxws.xsd">

    <context:component-scan base-package="https"/>
    <context:annotation-config />

    <import resource="classpath:META-INF/cxf/cxf.xml"/>

    <bean id="impl"

        class=
        "mil.army.sddc.ibs.ccr.datamgmt.webservice.DataMgmtWebServiceImpl"/>

    <bean  id="reasonCodeDAO"
        class="mil.army.sddc.ibs.ccr.dbwrapper.dao.RSNCodeDAO"       
        autowire="byName" >
        <constructor-arg
           value="jdbc:oracle:thin:@eipdzddevibs1:1526:CSSDEV"/>
        <constructor-arg value="CSS" />
        <constructor-arg value="s*awk:a_xa2wpt#a7jiv" />
        <constructor-arg value="oracle.jdbc.driver.OracleDriver" />
    </bean>
    <bean  id="ccrQueueDAO"
        class="mil.army.sddc.ibs.ccr.dbwrapper.dao.CCRQueueDAO"     
            autowire="byName"  >
        <constructor-arg
            value="jdbc:oracle:thin:@eipdzddevibs1:1526:CSSDEV"/>
        <constructor-arg value="CSS" />
        <constructor-arg value="s*awk:a_xa2wpt#a7jiv" />
        <constructor-arg value="oracle.jdbc.driver.OracleDriver" />
    </bean>

    <jaxws:endpoint
        id="dataMgmtWebService"
        implementor="#impl"
        address="/DataMgmt" />
</beans>

Web.xml

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">

   <display-name>CCRDataMgmt</display-name>
   <listener>
       <listener-class>
           org.springframework.web.context.ContextLoaderListener
       </listener-class>
    </listener>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>

    <!-- Session Timeout  -->
    <session-config>
        <session-timeout>120</session-timeout>
    </session-config>
</web-app>

This is pretty bizarre, and I'm sure there's something really dumb and obvious I'm missing that causes it.

Answers


Your bean names are not matching rsnCodeDAO and reasonCodeDAO. And since your injection is on instance field. At first it creates instance with constructor, at that point dependency is not injected yet. And with reflection it sets on the private properties.

You also have 2 bean definition for service, inside xml and with anotation. One with dependencies one without.

 <bean id="impl"   class=  "mil.army.sddc.ibs.ccr.datamgmt.webservice.DataMgmtWebServiceImpl"/>

and

@Service

Removing bean id="impl" from XML would probably make it work as you expect (one bean with dependencies)


Need Your Help

How do I move an existing Git submodule within a Git repository?

git git-submodules

I would like to change the directory name of a Git submodule in my Git superproject.

basicHttpBinding vs wsHttpBinding

.net wcf web-services wcf-binding mtom

In a WCF endpoint, what is the difference between basicHttpBinding and wsHttpBinding? When should each be used?