This week's giveaway is in the Programmer Certification (OCPJP) forum.
We're giving away four copies of Java Mock Exams (software) and have David Mayer on-line!
See this thread for details.
Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How does @Resource injection work?

Jayesh A Lalwani
Posts: 2762
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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,
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!