users@glassfish.java.net

Re: How to make the _at_Resources annotation work?

From: <glassfish_at_javadesktop.org>
Date: Wed, 28 Mar 2007 08:43:29 PST

Hi Rima,

There's a difference between declaring a resource dependency and declaring a resource
dependency that also results in injection. If you need injection, you'll have to use the
@Resource annotation on a field or method. The class-level Java EE environment
annotations only define a resource dependency that can later be retrieved through a
component environment lookup.

Taking your example, the resource dependencies could be looked up anywhere within the
web application as follows :

  InitialContext ic = new InitialContext();
  DataSource ds = (DataSource) ic.lookup("java:comp/env/myDB");
  ConnectionFactory cf = (ConnectionFactory) ic.lookup("java:comp/env/myJMSConn");

The name() attribute refers to the name of the entry within the component environment.
Since both of these dependencies refer to global resources you also need to make sure
they are correctly mapped to the JNDI name of global administered resource within
the app server. By default, the deployment code in Glassfish will assume that the
global JNDI name is the same as the contents of the name() attribute. Otherwise, you
can map the resource by using the @Resource mappedName() attribute or by mapping
the resource-ref in sun-web.xml, just as you would with a resource-ref.

There is more about the use of @EJB within our EJB FAQ here :

https://glassfish.dev.java.net/javaee5/ejb/EJB_FAQ.html

 ---ken
[Message sent by forum member 'ksak' (ksak)]

http://forums.java.net/jive/thread.jspa?messageID=210312