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.
The use of “
lookup” instead of “name” in this case is correct, as the entry is already defined in thejava:appnamespace 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@Resourceannotation using thelookupattribute: