Java Reference
In-Depth Information
@Stateless
public class DefaultBidService implements BidService {
...
@Resource(lookup="java:global/jdbc/ActionBazaarDB")
private DataSource dataSource;
The lookup element allows for direct JNDI lookup, and this code assumes the EE server
has been configured with a DataSource bound to this location. If this name is changed
or if the DataSource is bound to a different location in a different EE server, the injec-
tion will fail.
Injecting JMS resources
Recall the discussion on messaging and MDBs in chapter 4 . If your application has
anything to do with messaging, it's likely going to need to use JMS resources such
as javax.jms.Queue , javax.jms.Topic ,
javax.jms.QueueConnectionFactory , or javax
.jms.TopicConnectionFactory . Just like a JDBC DataSource , these resources
are stored in the application server's JNDI context and can be injected through the @Re-
source annotation. As an example, the following code injects a Queue bound to the
name "java:global/jms/ActionBazaarQueue" to the queue field:
@Resource(lookup="java:global/jms/ActionBazaarQueue")
private Queue queue;
Injecting EJBContext
Earlier we discussed the EJBContext , SessionContext , and MessageDriv-
enContext interfaces. One of the most common uses of resource injection is to gain ac-
cess to the EJBContext . The following code, used in the DefaultBidService ses-
sion bean, injects the EJB type-specific context into the context instance variable:
@Resource
private SessionContext 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. Instead, when the container detects the
@Resource annotation on the context variable, it figures out that the EJB context
specific to the current bean instance must be injected by looking at the variable data
Search WWH ::




Custom Search