[英]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上下文中运行。 你做完了吗?
很久以前了。 我在看我的代码,发现我放入了应该在春季启动的以下方法:
@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.