File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes EJB and other Java EE Technologies and the fly likes How does @Resource injection work? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "How does @Resource injection work?" Watch "How does @Resource injection work?" New topic

How does @Resource injection work?

Jayesh A Lalwani
Saloon Keeper

Joined: Jan 17, 2008
Posts: 2746

I am trying to write a app that will send messages to a message queue. RIght now, I am sending the messages through a servlet. It works, but I have no idea how the @Resource injection works.

My code is

I created the jms/testQueue and jms/connectionFactory in my Glassfish Admin console. The code works fine. I am trying to understand how it works.

I understand that the @Resource annotation is injecting the code that will look up and object of the name jms/connectionFactory in the JNDI. But, I don;t understand how it gets the JNDI context? If I want to write a standalone app that does the same thing, where do I put the reference to the JNDI context? Do I have to put it into a property file?

I am probably asking a stupid question, and am missing something that should be very obvious
Reza Rahman
Ranch Hand

Joined: Feb 01, 2005
Posts: 580

I don't think it is practical to answer the questions you pose in a forum post. I would suggest looking up a decent reference for EJB 3/Java EE 5.

In short, the container looks into the application server's default JNDI context for DI. You can change that by specifying a different file in the classpath. As to standalone applications, you can still use DI via an application client container or an embeddded EJB 3 container. Otherwise, you can get a handle to a JNDI context using the JNDI API.

Hope this helps,

Independent Consultant — Author, EJB 3 in Action — Expert Group Member, Java EE 6 and EJB 3.1
I agree. Here's the link:
subject: How does @Resource injection work?
It's not a secret anymore!