8 Replies Latest reply on Apr 28, 2011 4:53 PM by Istvan Viczian

    Referencing to global JNDI with @Resource annotation in a servlet

    Istvan Viczian Newbie



      I would like to access to a global JNDI resource (created with JNDIBindingServiceMgr) from a servlet in a WAR component of the EAR application (JBoss AS 5.1).

      I would like to use the @Resource annotation, but it doesn't work. The value of the attribute is always null.


      Given the following:


      - The global JNDI name is: jbossconfig/string (I see with the JNDIView MBean list() method result)

      - The local JNDI name is: s, defined in the jboss-web.xml with the following config:








      The resource annotation is in the servlet:


      @Resource(name = "s")

      private String s;


      And the value of the s is null.


      Interresting facts:

      - I could read the value from the global JNDI with lookup: ctx.lookup("jbossconfig/string");

      - I could read the value form the LOCAL JNDI (!!!) with lookup: ctx.lookup("java:comp/env/s");

      - @Resource(mappedName="jbossconfig/string") works (But it depends on global name - not on logical name, so it is not a good solution.)

      - I tried the resource-ref in web.xml, it doesn't work - it is not suprise, because the @Resource annotation is the equivalent of the resource-ref entry in web.xml

      - I tried to use the ResourceLink to link to the global jndi without success


      So, how can I access a global jndi with Resource annotation from servlet? Is it possible? (With mapped name it works!)



      I've attached the war file which contains the source of the servlet, too. And I've attached the jndi-binding-service.xml, which defines the global jndi names. Just copy both of them to the deploy directory, and try the /jbossconfig url.



      Thank you,