-
1. Referencing to global JNDI with @Resource annotation in a servlet
jaikiran Feb 28, 2011 2:43 AM (in response to viczi)What's the version of the web-app xsd in web.xml? It should be 2.5 minimum for injection to work in servlets.
-
2. Referencing to global JNDI with @Resource annotation in a servlet
viczi Feb 28, 2011 3:16 AM (in response to jaikiran)Yes, the version is 2.5, this is the root tag of the web.xml:
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
-
3. Referencing to global JNDI with @Resource annotation in a servlet
jaikiran Mar 1, 2011 2:04 AM (in response to viczi)Please post the exact code in the servlet and also can you try this against JBoss AS 6.0.0.Final?
-
4. Re: Referencing to global JNDI with @Resource annotation in a servlet
viczi Mar 1, 2011 8:32 AM (in response to jaikiran)Hello!
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.
It doesn't work in JBoss AS 6.0.0.Final.
Source of the servlet:
@Resource(name = "s") private String s; @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Context c; try { c = new InitialContext(); System.out.println("Global lookup: " + c.lookup("jbossconfig/string")); System.out.println("Local lookup: " + c.lookup("java:comp/env/s")); } catch (NamingException ex) { ex.printStackTrace(); } System.out.println("DI: " + s); }
The result is:
Global lookup: Hello, JNDI!
Local lookup: Hello, JNDI!
DI: null
-
5. Re: Referencing to global JNDI with @Resource annotation in a servlet
jaikiran Mar 1, 2011 9:23 AM (in response to viczi)Istvan Viczian wrote:
- The local JNDI name is: s, defined in the jboss-web.xml with the following config:
<resource-ref>
<res-ref-name>s</res-ref-name>
<res-type>java.lang.String</res-type>
<jndi-name>jbossconfig/string</jndi-name>
</resource-ref>
Change it to:
<jboss-web> <env-entry> <env-entry-name>s</env-entry-name> <env-entry-type>java.lang.String</env-entry-type> <jndi-name>jbossconfig/string</jndi-name> </env-entry> </jboss-web>
A @Resource of type java.lang.String is an env-entry.
-
6. Re: Referencing to global JNDI with @Resource annotation in a servlet
viczi Apr 28, 2011 4:46 PM (in response to jaikiran)Hello!
Thank you very much, Ill' give it a try!
But what about the EJB layer? I use the following, see above. As you see, it is a
resource-ref, and doesn't work! What should I use in the EJB layer? resource-ref, or resource-env-ref?
As I see, there is no "env-entry" there.
<?xml version=
"1.0"
encoding=
"UTF-8"
?>
<jboss>
<enterprise-beans>
<session>
<ejb-name>JBossConfigBean</ejb-name>
<resource-ref>
<res-ref-name>jbossconfig/string</res-ref-name>
<res-type>java.lang.String</res-type>
<jndi-name>jbossconfig/string</jndi-name>
</resource-ref>
</session>
</enterprise-beans>
</jboss>
-
7. Referencing to global JNDI with @Resource annotation in a servlet
jaikiran Mar 2, 2011 3:15 AM (in response to viczi)For java.lang.String type, it should always be a env-entry in the deployment descriptors (doesn't matter whether it's web or EJB).
Istvan Viczian wrote:
env-entry" there.
You mean, you don't see an env-entry element in the jboss.xml xsd? It's there and you should be able to use it similar to the jboss-web.xml.
-
8. Re: Referencing to global JNDI with @Resource annotation in a servlet
viczi Apr 28, 2011 4:53 PM (in response to jaikiran)Hello!
In the jboss_5_1.xsd (http://www.jboss.org/j2ee/schema/jboss_5_1.xsd) I don't find the env-entry. The XML validator shows error.
I've read the specification, and the is no solution to access the variables defined in the application server, in the GLOBAL JNDI. With the @Resource annotation you can access to simple variables (String, Character, Byte, Short, Integer, Long, Boolean, Double and Float), but it can be defined only in the ejb-jar.xml, with env-entry tag, not in application server.
Interresting thing, that JBoss 5.1 and 6 works different.