Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JNDI injection of app name doesn't work, lookup does

This doesn't seem to work (=null):

@Resource(name = "java:app/AppName")
private String appName;

But a lookup of the same name does:

appName = (String) new javax.naming.InitialContext().lookup("java:app/AppName");

I found many examples having a lookup property in @Resource instead of name. However, I can't find anything about that, I'm quite confident it should be name, the former isn't part of the spec.

FYI, I'm using Glassfish 3.1 and I'm accessing appName from a @PostConstruct method in a singleton bean.

like image 844
ymajoros Avatar asked Jan 30 '12 11:01

ymajoros


1 Answers

The use of "lookup" instead of "name" in this case is correct, as the entry is already defined in the java:app namespace and is merely being looked up, rather than defined. This is part of the Java EE 6 specification (which Glassfish 3.1 implements): @Resource#lookup().

Alternative to looking up the app name using InitialContext, you can also use the @Resource annotation using the lookup attribute:

@Resource(lookup = "java:app/AppName")
private String appName;
like image 174
shelley Avatar answered Sep 29 '22 11:09

shelley