我正在尝试使用JBossWS(本地堆栈)公开Web服务,并且还利用Spring的依赖注入。这是我的代码的精简版:

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时,它可以很好地启动,并且我可以看到Spring正在预先实例化我的类,但是当我向Web服务发出调用时,endpointEcho可以按预期工作,而serviceEcho抛出NullPointerException。似乎当JBossWS实例化终结点类时,并没有发现我的Spring配置。有什么简单的方法可以告诉JBossWS有关Spring的信息吗?我觉得我要么错过了一些很小的细节,要么就走错了。有任何想法吗?

最佳答案

您的服务必须扩展SpringBeanAutowiringSupport才能使用自动装配支持。

10-07 15:42