从@WebService获取对spring bean的引用

时间:2009-05-25 16:56:59

标签: java web-services spring cxf

我正在使用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?

有人有想法吗?

由于 阿龙

2 个答案:

答案 0 :(得分:1)

如果您使用的是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的服务

答案 1 :(得分:0)

很久以前。 我正在查看我的代码,我看到我提出了以下方法 应该在春天开始:

@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();

}

请告诉我它是否解决了你的问题....

相关问题