• Post Reply Bookmark Topic Watch Topic
  • New Topic

JNDI and java.annotation.Resource

 
Jeff Gaer
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I apologize if I should ask this in the EJB or JBOSS forums, but it seemed to be a general @Resource questions. I am not sure I understand how the @Resource annotation works. I am running on java 1.7, JEE 6, and JBOSS AS 7.1.

The javax.annotation.Resources documentation indicates that the name parameter is the JNDI name of the resource. But lookups that fail using a JNDI name and thev@Resource annotation succeed when a JNDI context is obtained and used to look up the resource. For example in the code below I can successfully get a mail session if I comment out the @Resource annotation and uncomment the @PostConstruct annotation. In both cases the string I use for JNDI name is "mail/yahoo"



If I run the code as shown attempting to use the @Resource annotation jboss reports the following error:

JBAS014775: New missing/unsatisfied dependencies:
service jboss.naming.context.java.comp.EJB05EAR.CHAP05EJB.StatusMailerBean.env.mail.yahoo (missing) dependents: [service jboss.deployment.subunit."EJB05EAR.ear"."CHAP05EJB.jar".component.StatusMailerBean.START]

The configuration entry in standalone.xml for the mail session is

<mail-session jndi-name="java:/mail/yahoo" from="jeff_gaer@yahoo.com">
<smtp-server ssl="true" outbound-socket-binding-ref="mail-smtp-yahoo">
<login name="jeff_gaer" password="xxxxxxxxxxx"/>
</mail-session>

I have a similar problem with otherJNDI lookups. I haven't used the @Resource annotation outside of the context of the examples in the "Beginning EJB3" (apress publications),
 
Rob Spoor
Sheriff
Posts: 20816
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try using the lookup attribute with the full JNDI name, including the java:// prefix:
I did that recently on a project and it worked without a problem.
 
Jaikiran Pai
Sheriff
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a common confusion. Previous discussions on this can be found here, which might help understand what those attributes mean:

http://www.coderanch.com/t/164146/java-EJB-SCBCD/certification/Resource#796865
http://www.coderanch.com/t/622076/java-EJB-SCBCD/certification/Resource-lookup

P.S: In those discussions there's a reference to the mappedName attribute. You can/should replace that by "lookup" attribute (which is available in newer versions) as Rob noted in his post.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!