Spring @AutoWired always null

为君一笑 提交于 2019-12-24 06:36:15

问题


I'm trying to call SOAP service from another system on my program. I've generated the java classes needed from WSDL by using wsimport command.

Here's my program's configuration

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>


and here's my xml configuration file where i store the config location


UPDATE: added ESCreditRunner class as a bean below, and still got the same error

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>


and here's how i use the @AutoWired, to call the SOAP service topupWebService.. on the testCall() method


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");
        }
    }

when i run the testCall() method, i always get NullPointerException on the topupWebService variable. It looks like it's not injected properly..

what i'm missing here?


Update: i've made another thread as a continuation of this thread. Kindly check it here :) Spring WebService with Endpoint vs no Endpoint

回答1:


You should set the annotation @Component on the class ESCreditRunner




回答2:


You can try to use:

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

If your xml isn't in classpath, then you can use classpath:**/cxf-servlet.xml




回答3:


I have solved using @Component on my class and setting @Autowired to all object called in cascade from the controller or check your package in settings like that spring mvc @Autowired error Unsatisfied 'required' dependency of type



来源:https://stackoverflow.com/questions/44900195/spring-autowired-always-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!