background image

Class-Based Injection

<< Method-Based Injection | Running the confirmer Example >>
<< Method-Based Injection | Running the confirmer Example >>

Class-Based Injection

...
}
In the code above, the JNDI name is customerDB, and the inferred type is
javax.sql.DataSource.class
.
Class-Based Injection
To use class-based injection, decorate the class with a @Resource annotation, and set the
required name and type elements.
@Resource(name=
"myMessageQueue",
type=
"javax.jms.ConnectionFactory")
public class SomeMessageBean {
...
}
Declaring Multiple Resources
The @Resources annotation is used to group together multiple @Resource declarations for
class-based injection.
@Resources({
@Resource(name=
"myMessageQueue",
type=
"javax.jms.ConnectionFactory"),
@Resource(name=
"myMailSession",
type=
"javax.mail.Session")
})
public class SomeMessageBean {
...
}
The code above shows the @Resources annotation containing two @Resource declarations. One
is a JMS message queue, and the other is a JavaMail session.
The confirmer Example Application
The confirmer example application demonstrates how to use an injected JavaMail session to
send a confirmation email.
If you've ever ordered a product from a web site, you've probably received an email confirming
your order. The ConfirmerBean class demonstrates how to send email from an enterprise bean.
The confirmer Example Application
The Java EE 5 Tutorial · September 2007
1014