views:

436

answers:

3

Hi,

I'm using CXF to generate a web service from wsdl. The generated web service has the annotation @WebService How do i get a reference to spring bean from the web service? All my spring beans are annotated with @Service, and I can access them in my web application. How do 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 the getWebApplicationContext method returns null

When I replace getWebApplicationContext with getRequiredWebApplicationContext I get an error message: No WebApplicationContext found: no ContextLoaderListener registered?

Anyone has an idea?

Thanks Alon

+1  A: 

If you were using JUnit 4.4 or higher you could inject it using the 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 that the web service be running in a web context when the test is started. Have you done that?

You also might prefer testing WSDL based services using SOAP UI.

duffymo
I don't want to use JUnit for this task, as it not seems to me as the standard way to solve it.Anyway, I use SOAP UI tool and it's indeed very helpful
Alon
A: 

Alon,

Im running into the same problem that you raised here.. how did you solve it can you elaborate ?

Will

wmitchell
A: 

It was a long time ago. I'm looking on my code and I see that I put the following method that should init the 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 solve you problem....

Alon