Skip to main content

@Resource access to JNDI custom String resource doesn't work [GF 3.1]

2 replies [Last post]
tamm0r
Offline
Joined: 2010-01-12
Points: 0

Hi,
I created a JNDI custom java.lang.String resource named "jndiTest" using the built-in PrimitivesAndStringFactory. Accessing the resource with the following code works as expected:

Context ctx = new InitialContext();
String jndiTest = (String) ctx.lookup("jndiTest");

// prints "jndiTestValue" to log
System.out.println(jndiTest);

However, trying to access the resource via @Resource annotation does not work, but only for strings and primitives:
@Resource(name="jndiTest")
private String jndiTest;

private void test() {
    // prints "null" to log
    System.out.println(jndiTest);
}<br type="_moz" />

Now comes the weird part: if I change the resource type and the variable type to Properties (and thus to PropertiesFactory) I can access the resource without any problems:
@Resource(name=&quot;jndiTest&quot;)
private Properties jndiTest;

private void test() {
    // prints &quot;{value=jndiTestValue}&quot; to log
    System.out.println(jndiTest);
}<br type="_moz" />

Any ideas how to fix that?
-Marian

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
cf126330
Offline
Joined: 2005-03-29
Points: 0

The preferred way to inject it is:

@Resource(lookup=&quot;jndiTest&quot;)

To use @Resource.lookup, set -Djava.endorsed.dirs=$GF_HOME/modules/endorsed to override the common annotation classes in Java SE.

tamm0r
Offline
Joined: 2010-01-12
Points: 0

Thanks, works like a charm!

Another useful link on this topic: http://javahowto.blogspot.com/2010/04/resourcelookup-and-classpath.html

-Marian