JNDI injection of application name not working, search

This does not work (= null):

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

But search with the same name:

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

I found many examples having a search property in @Resource instead of a name. However, I can not find anything about this, I am quite sure that this should be a name, the first is not included in the specification.

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

+4
source share
1 answer

Using " lookup " instead of " name " in this case is correct, since the entry is already defined in the java:app namespace and is just browsed, not defined. This is part of the Java EE 6 specification (which Glassfish 3.1 implements): @Resource#lookup() .

Alternatively, you can also use the @Resource annotation using the lookup attribute to search for the name of the application using InitialContext :

 @Resource(lookup = "java:app/AppName") private String appName; 
+4
source

Source: https://habr.com/ru/post/1393744/


All Articles