Skip to main content

How to make the @Resources annotation work?

2 replies [Last post]
rpatel
Offline
Joined: 2003-08-13

Hello,

I have spent better part of two hours trying to make this thing work.

I am trying to do injection of multiple resources within my Servlet class. The code is something like this:

// all the relevant import statements

@Resources ({
@Resource(name="myDB" type=javax.sql.DataSource),
@Resource(name="myJMSConn" type=javax.jms.ConnectionFactory)
})
public class MyServlet extends javax.servlet.HTTPServlet
{
// Relevant Servlet code
}

Now what exactly am I doing wrong here? Can someone please help me with the syntax? There are almost no examples of using @Resources (or a very similar @EJBs) annotation.

Thanks,
Rima.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
ksak
Offline
Joined: 2005-05-20

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

rpatel
Offline
Joined: 2003-08-13

Hello Ken,

Yes, of course, I missed the point that @Resources (as well as @EJBs) annotation is for declaring the dependency on a resource and not for injecting the resource itself. Thank you for making this crucial point.

Now, I finally figured out why my code (given in original post) would not compile. My syntax is wrong. Given here is the correct syntax:

@Resources ({
@Resource(name="myDB", type=javax.sql.DataSource.class),
@Resource(name="myJMSConn", type=javax.jms.ConnectionFactory.class)
})
public class MyServlet extends javax.servlet.HTTPServlet
{
// Relevant Servlet code
}

Note that I got the original syntax from the code snippet given in the Common Annotations spec (the spec has it wrong).

Thank you Ken!

Regards,
Rima.