3
我想使用JBossWS(本地堆栈)公开Web服务,并利用Spring的依赖注入。这里是我的代码净化的缩小版本:JBossWS中的弹簧配置
web.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>
的applicationContext.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>
EndpointService.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);
}
}
TestService.java:
package com.blah.webservice;
import org.springframework.stereotype.Service;
@Service
public class TestService {
public TestService() {}
public String serviceEcho(String echo) {
return echo;
}
}
当我建立这个并部署到JBoss,它启动就好了,我可以看到春天预实例化我的课,但是当我发布到Web服务调用, endpointEcho按预期工作,而serviceEcho引发NullPointerException。看起来,当JBossWS实例化端点类时,它没有发现我的Spring配置。有没有一种简单的方法可以告诉JBossWS Spring?我觉得我要么缺少一些非常小的细节,要么我接近这一切都是错误的。有任何想法吗?
我试过了。试图调用testService仍然给我一个NullPointerException。似乎自动装配正在发生,但可能是在错误的情况下。 – bhinks
所以我又试了一次(第三次是一种魅力,对吗?),它似乎正在工作。我将端点类的构造函数中的@Autowired注释移到了各个属性声明中,现在我正在摇摆和滚动。谢谢! – bhinks