Get spring bean reference from @WebService
I am using CXF to create a web service from wsdl. The generated web service has @WebService annotation How to get a reference to a spring bean from a web service? All my spring beans are annotated with @Service and I can access them in my web application. How can I access them also from my web service?
I've tried the following:
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";
}
}
But getWebApplicationContext method returns null
When I replace getWebApplicationContext with getRequiredWebApplicationContext I get the error: No WebApplicationContext found: Is the ContextLoaderListener registered?
Anyone have an idea?
Thanks Alon
If you were using JUnit 4.4 or higher, you can introduce it using Spring 2.5 JUnit annotations. Here's an example.
http://hamletdarcy.blogspot.com/2008/12/autowired-junit-tests-with-spring-25.html
Of course, it goes without saying that this approach requires the web service to run in the web context when the test runs. Did you do it?
You may also prefer to test WSDL based services using SOAP UI.
a source to share
It was a long time ago. I go through my code and I can see that I have applied the following method which should initialize spring:
@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();
}
Please tell me if it solves your problem ....