• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

JEE6 - Inject ApplicationScoped bean in WebService

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm building a web service (first time) with JEE6 and cannot seem to inject a bean that is an application scoped utility class to use within the web service method call. In the debugger, the injected object is null. Below is some basic skeleton code:




I've tried adding @Named to each class but it didn't fix this. Is it even possible to access an application scoped bean from a web service? Thanks for any tips.
 
Mark Ericksen
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Replying to my own question with a work-around and more details for those smarter than me to help understand what's going on.

I don't know for sure that using @Inject won't work because the Application Scoped bean is never getting initialized. I guess I was assuming the container was handling the life-cycle of these "managed" beans, when in fact the container does not. If this project was utilizing JSF, however, the FacesContext (or whatever) will handle the life-cycle and the Application Scoped beans do get created. This just isn't true for Web Services I'm finding out.

I don't know if there's a way to manually bootstrap the "managed bean" life-cycle system. If there is, I'd love to know how. It would be useful to do so for Java tool (i.e. no container) projects.

What I did to get around this was to inject EJBs instead. Since this is a new project I have the luxury to do what it takes to get it to work. Since the container manages the life-cycle of the EJB, the bean will get instantiated and be ready for injection without any hassles. In my case the utility objects are annotated as @Singleton EJBs and I use the @EJB injection annotation in the @WebService beans.

It seems to be working with this work around. I just hope more work is done on JEE to makes "bean management" more integrated with fewer gotchas. Cheers!
 
Look ma! I'm selling my stuff!
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic