Java Reference
In-Depth Information
tag are limited to these Java types:
String
,
Character
,
Byte
,
Short
,
Integer
,
Long
,
Boolean
,
Double
, and
Float
. Because environment entries are accessible via
JNDI they can be injected by name. As you might gather, the data types of the environ-
ment entry and the injected variable must be compatible. Otherwise, the container throws
a runtime exception while attempting injection. Note that if you have complex DI-based
configuration needs, you should most likely look into CDI. CDI is covered in more depth
in
section 5.3.8
.
Injecting email resources
In addition to JDBC data sources and JMS resources, the other heavy-duty resource Enter-
prise application often used is the JavaMail API
javax.mail.Session
. JavaMail ses-
sions abstract the email server configuration and are stored in the application server JNDI
registry. The
Session
can be injected into an EJB with the
@Resource
annotation and
used to send email. In the ActionBazaar application, this is useful for sending the winning
bidder a notification after bidding on an item is over. The code to inject the mail
Session
looks like this:
@Resource(lookup="java:global/mail/ActionBazaar")
private javax.mail.Session mailSession;
We'll leave the deployment descriptor configuration of a mail session as an exercise. You
can find the one-to-one mapping between annotations and deployment descriptors in ap-
pendix A.
Injecting the timer service
The container-managed timer service gives EJBs the ability to schedule tasks in a simple
way (you'll learn more about timers in
chapter 7
). You inject the container timer service
into an EJB using the
@Resource
annotation:
@Resource
private javax.ejb.TimerService timerService;
Just as with the EJB context, the timer service isn't registered in JNDI, but the container
resolves the resource by looking at the data type of the injection target.