Spring autowire工作,然后没有

时间:2015-12-04 18:29:39

标签: java spring

我的春天自动装配似乎失败了,所以我做了一个postconstruct方法来打印出发生的事情。结果我的服务impl类被实例化两次,第二次自动装配失败。

Here's the console output.

这是我的impl类的这一部分生成的

@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>

这很奇怪,而且我确定有些东西真的很愚蠢而且很明显我错过了它。

1 个答案:

答案 0 :(得分:0)

您的bean名称与rsnCodeDAOreasonCodeDAO不匹配。因为你的注射是在实例领域。首先,它使用构造函数创建实例,此时尚未注入依赖项。并通过反思设置私人财产。

您还有2个服务bean定义,xml内部和anotation。一个没有依赖的一个。

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

@Service

从XML中删除bean id="impl"可能会使它按预期工作(一个具有依赖项的bean)