Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

@Resource For A Non-EJB

 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I have written a standard Java class (not an EJB) which I have packaged as a JAR within an EAR. This Java Class will put things on a queue when requested.

Rather than perform a manual JNDI lookup for the Queue and Connection Factory, I used the @Resource annotation, and assumed that because it was within a Container (GlassFish) that this would work.
I am pretty confident that the annotation/queue/connectionfactory is set up correctly because the deployment through NetBeans fails otherwise.
However, I keep getting a NullPointerException when the code is executed, so the @Resource annotation isn't working.

Does the @Resource annotation only work in EJBs? Or is this a GlassFish quirk?

Thanks,

MG
 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mark Garland wrote:
Does the @Resource annotation only work in EJBs? Or is this a GlassFish quirk?



@Resource and other similar EE injection constructs work only with managed classes like servlets, EJBs, managed beans etc. They won't work with plain Java classes.
 
Mark Garland
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jaikiran,

Thanks for your quick and accurate reply.

I've removed the @Resource annotations and put in a manual JNDI lookup, and all is working great - thanks!

The @Resource annotation not working in a POJO which is sat in a container feels a bit of a shame if honest. A lot of the annotations are great for reducing repeating boilerplate code, yet I'd be forced to convert my POJOs to EJBs if I wanted to use them even though the container is capable. Just feels like a missed opportunity to me.

Thanks once again for your help - much appreciated.

MG
 
Jaikiran Pai
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mark Garland wrote: A lot of the annotations are great for reducing repeating boilerplate code, yet I'd be forced to convert my POJOs to EJBs if I wanted to use them even though the container is capable. Just feels like a missed opportunity to me.



Java EE6 introduces Managed Beans which plugs this gap. A Managed Bean is a POJO which can have injection, lifecycle callbacks and interceptors. It however doesn't have any transactional, security related semantics unlike EJBs - which is understandable.
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I tried to use resource injection in a servlet with name parameter set to jndi name (which works in manual look up) in JBoss 4.2.x.x.

It is not working. Properties are null at run time.
 
You’ll find me in my office. I’ll probably be drinking. And reading this tiny ad.
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic