簡體   English   中英

從@WebService獲取對spring bean的引用

[英]Get reference to spring bean from @WebService

我正在使用CXF從wsdl生成Web服務。 生成的Web服務帶有注釋@WebService如何從Web服務獲取對spring bean的引用? 我所有的spring bean都用@Service注釋,我可以在Web應用程序中訪問它們。 如何從我的Web服務訪問它們?

我嘗試了以下方法:

public class TestWSImpl implements TestWSSoap{

    @Resource
    public WebServiceContext wsContext;

    @Override
    public String getTest() {

        ServletContext servletContext= (ServletContext) wsContext.getMessageContext().get(MessageContext.SERVLET_CONTEXT);

        ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);

        return "Test";
    }
}

但是getWebApplicationContext方法返回null

當我用getRequiredWebApplicationContext替換getWebApplicationContext時,我收到一條錯誤消息:沒有找到WebApplicationContext:沒有注冊ContextLoaderListener?

有人有主意嗎?

謝謝阿隆

如果您使用的是JUnit 4.4或更高版本,則可以使用Spring 2.5 JUnit批注將其注入。 這是一個例子。

http://hamletdarcy.blogspot.com/2008/12/autowired-junit-tests-with-spring-25.html

當然,不用說,這種方法要求在測試開始時Web服務在Web上下文中運行。 你做完了嗎?

您還可能更喜歡使用SOAP UI測試基於WSDL的服務

很久以前了。 我在看我的代碼,發現我放入了應該在春季啟動的以下方法:

@PostConstruct
public void init(){
    JaxWsServerFactoryBean svrFactory = new JaxWsServerFactoryBean();
    svrFactory.setServiceClass(MyWebService.class);
    svrFactory.setAddress("http://address.com");
    svrFactory.setServiceBean(wsHandler);
    svrFactory.getInInterceptors().add(new LoggingInInterceptor());
    svrFactory.getOutInterceptors().add(new LoggingOutInterceptor());
    svrFactory.create();

}

請告訴我是否可以解決您的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM