Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How does @Resource injection work?

 
Jayesh A Lalwani
Rancher
Posts: 2756
32
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
author
Ranch Hand
Posts: 580
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jayesh,

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 jndi.properties 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,
Reza
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic