Hi, Edwin and everyone.
I am reading EJB in Action. On p.133,
@Resource annotation elements:
name - besides performing an injection, the @Resource annotation implicitly creates a binding referring to the injected resource in the java:comp namespace. This is primarily done for backward compatibility. This attribute allows you to specify the name that is used for the implicit binding. This is equivalent t the <res-ref-name> element in deployment descriptors used extensively in EJB2.
My comment : I guess @Resource(name = ...) is not commonly used with EJB 3.1 unless it is used for backward compatibility with EJB 2.0.
lookup - This is the actual JNDI lookup name of the resource to be injected. This is likely the attribute you will use the most.
My comment: I guess it is always the right choice to use lookup for looking up the JNDI name of a queue, DataSource, Session and etc.
Example on p.134-135:
1. @Resource (lookup="java:global/jdbc/ActionBazaarDB")
private DataSource dataSource;
2. @Resource (lookup="java:global/jms/ActionBazaarQueue")
private Queue queue;
3. @Resource (lookup="java:global/mail/ActionBazaar")
private javax.mail.Session mailSession;
Note:
@Resource
private EJBContext context;
Note that the injected session context isn't stored anywhere in JNDI. In fact, it would be incorrect to try to specify JNDI lookup parameters in this case at all, and servers will probably ignore the element if specified.
mappedName - A vendor-name for the bean.
Example on p.21 of EJB 3.1 Cookbook by Richard Reese
The connection factory was created by the server GlassFish in this case, with the name jms/SalutationQueueFactory which is of type QueueConnectionFactory.
@Resource (mappedName ="jms/SalutationQueueFactory")
private QueueConnectionFactory queueConnectionFactory;
The queue is of type javax.jms.Queue and used the name jms/SalutationQueue as defined within the server. @Resource annotation was used again to inject this resource.
@Resource(mappedName="jms/SalutationQueue")
private Queue queue;
My comment: The application server, GlassFish , creates the resource QueueConnectionFactory with the name jms/SalutionQueueFactory and Queue with the name jms/SalutationQueue first before the developer can inject them.
Also, these name jms/... are not portable JNDI name and vendor-specific. According to
http://thegreyblog.blogspot.com/2010/09/introduction-to-ejb-30-injection-and.html, in EJB 3.1, mappedName is becoming deprecated.
mappedName
The EJB 3.0 Specification defines mappedName as a "product-specific name that the session bean should be mapped to." Often, application server use mappedName to map a session bean to a global JNDI name. The EJB 3.1 specification sort of deprecates the mappedName element and introduces the concept of "portable global JNDI name".
These are just my comments. Correct me if I am wrong.