2

I want to expose some functionality of my existing JEE7 web app using a SOAP based @WebService. Can/should this service inject my app's CDI beans? How do @RequestScoped, @SessionScoped, and @ApplicationScoped CDI beans behave, given that there's no HttpServletRequest to identify the current session or request? ie. How do they get looked up?

My observations:

  • @ApplicationScoped seems to work as expected
  • @SessionScoped seems to behave like request scope in that a new bean is created each time the web service is called, but it is subsequently accessible from other beans up until the service completes
  • @RequestScoped beans don't seem to be looked up property - eg. if my webservice injects two @RequestScoped beans, then one of those injects the other one, a new instance is created, rather than the first one being injected

My idea was to use the @Dependent annotation to make my session beans behave like request beans when injected into my @WebService, but this isn't working due to the request scoped behaviour noted above. I could just use my @SessionSoped beans as they are, but I'm concerned about the memory overhead since a new bean would be created for every web service request, and then persist for some 30 minutes or so, rather than being destroyed once the service has completed.

Any clarification or ideas would be appreciated!

Daniel Loiterton
  • 5,073
  • 5
  • 33
  • 46
  • For what it's worth, JAX-WS `@WebService` instance should absolutely have access to the `HttpServletRequest` via `@Resource` injection of `WebServiceContext` and the `MessageContext` from there: followed by `HttpServletRequest request = (HttpServletRequest) messageContext.get(MessageContext.SERVLET_REQUEST);` Refer to example [here](http://stackoverflow.com/questions/133436/how-can-i-get-access-to-the-httpservletrequest-object-when-using-java-web-servic). I suggest a code sample for further review. – Scott Heaberlin Oct 28 '14 at 22:58

0 Answers0