2011-03-14 263 views
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?我覺得我要麼缺少一些非常小的細節,要麼我接近這一切都是錯誤的。有任何想法嗎?

回答

2

您的服務必須擴展SpringBeanAutowiringSupport才能夠利用自動裝配支持。

+0

我試過了。試圖調用testService仍然給我一個NullPointerException。似乎自動裝配正在發生,但可能是在錯誤的情況下。 – bhinks

+0

所以我又試了一次(第三次是一種魅力,對嗎?),它似乎正在工作。我將端點類的構造函數中的@Autowired註釋移到了各個屬性聲明中,現在我正在搖擺和滾動。謝謝! – bhinks