Spring @AutoWired永远为null

时间:2017-07-04 07:59:04

标签: java xml spring web-services soap

我试图在我的程序中从另一个系统调用SOAP服务。我使用wsimport命令生成了WSDL所需的java类。

这是我的程序配置

web.xml

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

    <display-name>PTDAM Promo Engine</display-name>
    <description>Promo Engine using Elastic Search</description>

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

    </context-param>

    <context-param>
        <param-name>webAppRootKey</param-name>
        <param-value>promoengine</param-value>
    </context-param>

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

    <servlet>
        <servlet-name>CXFServlet</servlet-name>
        <display-name>CXF Servlet</display-name>
        <servlet-class>
            org.apache.cxf.transport.servlet.CXFServlet
        </servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>CXFServlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>


</web-app>


这是我存储配置位置的xml配置文件


更新:将ESCreditRunner类添加为下面的bean,但仍然出现相同的错误

的cxf-servlet.xml中

    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxrs="http://cxf.apache.org/jaxrs"
           xmlns:cxf="http://cxf.apache.org/core" xmlns:jaxws="http://cxf.apache.org/jaxws"
           xmlns:sec="http://cxf.apache.org/configuration/security" xmlns:http="http://cxf.apache.org/transports/http/configuration"
           xmlns:http-conf="http://cxf.apache.org/transports/http/configuration"

           xsi:schemaLocation="http://www.springframework.org/schema/beans
                      http://www.springframework.org/schema/beans/spring-beans.xsd 
                      http://cxf.apache.org/transports/http/configuration
                      http://cxf.apache.org/schemas/configuration/http-conf.xsd
                  http://cxf.apache.org/configuration/security
                  http://cxf.apache.org/schemas/configuration/security.xsd
                  http://cxf.apache.org/jaxrs 
                  http://cxf.apache.org/schemas/jaxrs.xsd 
                  http://cxf.apache.org/core 
                  http://cxf.apache.org/schemas/core.xsd
                  http://cxf.apache.org/jaxws 
                  http://cxf.apache.org/schemas/jaxws.xsd
                  http://www.springframework.org/schema/context 
                  http://www.springframework.org/schema/context/spring-context-3.0.xsd">        

         <!-- use this to enable @AutoWired -->
         <context:annotation-config />
         <context:component-scan base-package="ptdam.emoney.webservice.transactions" />

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

        <!-- Client -->
        <jaxws:client id="topupWebService"
            serviceClass="ptdam.emoney.webservice.transactions.TopupWebService"
            address="http://localhost:8080/ecash-core-engine-dev-mitigasi/services/emoneytopup"

<!-- Beans -->
    <bean id="esCreditRunner" class="com.ptdam.promo.services.ESCreditRunner" />
        />      
    </beans>


以及我如何使用@AutoWired来调用topupWebService方法上的SOAP服务testCall() ..


ESCreditRunner.java

package ptdam.emoney.webservice.transactions;    
public class ESCreditRunner {
        private File statusFile;
        private Properties status;
        private static final Logger logger = Logger.getLogger(ESCreditRunner.class);


        @Autowired
        private TopupWebService topupWebService;
        public void setTopupWebService(TopupWebService topupWebService) {
            this.topupWebService = topupWebService;
        }

        // testing 04/07/2017
        public void testCall(){
            topupWebService.echoTest("Luki");
        }
    }

当我运行testCall()方法时,我总是在topupWebService变量上得到NullPointerException。它看起来没有正确注入..

我在这里失踪了什么?

<小时/> 更新: 我已经将另一个线程作为该线程的延续。请在这里查看:) Spring WebService with Endpoint vs no Endpoint

3 个答案:

答案 0 :(得分:0)

您应该在课程@Component

上设置注释ESCreditRunner

答案 1 :(得分:0)

您可以尝试使用:

 @RunWith(SpringJUnit4ClassRunner.class)
 @ContextConfiguration(locations = "cxf-servlet.xml")

如果您的xml不在类路径中,那么您可以使用classpath:**/cxf-servlet.xml

答案 2 :(得分:-1)

我已经解决了在我的类上使用 @Component 并将 @Autowired 设置为从控制器级联调用的所有对象,或者在{{3 }}