我正在尝试使用 JBossWS(本机堆栈)公开 Web 服务,并利用 Spring 的依赖注入。这是我的代码的精简版本:
网页.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<display-name>Test Service</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>EndpointService</servlet-name>
<servlet-class>com.blah.webservice.EndpointService</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>EndpointService</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
应用程序上下文.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=" http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:spring-configured />
<context:load-time-weaver />
<context:annotation-config />
<context:component-scan base-package="com.blah.webservice" />
</beans>
端点服务.java
package com.blah.webservice;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
@WebService
@SOAPBinding(parameterStyle=SOAPBinding.ParameterStyle.BARE)
public class EndpointService {
private TestService testService;
public EndpointService() {}
@Autowired
public EndpointService(TestService testService) {
this.testService = testService;
}
@WebMethod
public String endpointEcho(String echo) {
return echo;
}
@WebMethod
public String serviceEcho(String echo) {
return testService.serviceEcho(echo);
}
}
测试服务.java:
package com.blah.webservice;
import org.springframework.stereotype.Service;
@Service
public class TestService {
public TestService() {}
public String serviceEcho(String echo) {
return echo;
}
}
当我构建它并部署到 JBoss 时,它启动得很好,我可以看到 Spring 正在预实例化我的类,但是当我发出对 Web 服务的调用时,endpointEcho 按预期工作,而 serviceEcho 抛出 NullPointerException。似乎当 JBossWS 实例化端点类时,它并没有发现我的 Spring 配置。有没有一种简单的方法可以告诉 JBossWS 关于 Spring 的信息?我觉得我要么错过了一些非常小的细节,要么我正在接近这一切都是错误的。有任何想法吗?