gpt4 book ai didi

java - Spring @AutoWired 始终为 null

转载 作者:行者123 更新时间:2023-12-01 06:05:53 25 4
gpt4 key购买 nike

我正在尝试从我的程序上的另一个系统调用 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 在 testCall() 方法上调用 SOAP 服务 topupWebService..


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。看起来没有正确注入(inject)..

我在这里缺少什么?

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

最佳答案

您应该在类ESCreditRunner上设置注释@Component

关于java - Spring @AutoWired 始终为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44900195/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com