Java Reference
In-Depth Information
5.1.3. Accessing the container environment through the EJB context
You get access to the
EJBContext
itself through dependency injection. For example, a
SessionContext
could be injected into a session bean as follows:
@Stateless
public class DefaultBidService implements BidService {
@Resource
SessionContext context;
...
}
In this code snippet, the container detects the
@Resource
annotation on the
context
variable and figures out that the bean wants an instance of its session context. A more de-
tailed discussion of the
@Resource
annotation will be described later in the chapter.
Much like a session context, a
MessageDrivenContext
can be injected into a MDB
as follows:
@MessageDriven
public class OrderBillingProcessor {
@Resource
MessageDrivenContext context;
...
}
Note
It's illegal to inject a
MessageDrivenContext
into a session bean or a
Ses-
sionContext
into an MDB.
This is all the time we need to spend on the EJB context right now. Now let's turn our at-
tention to a vital part of EJB 3—dependency injection (DI). We provided a brief overview
of DI in
chapter 2
and have been seeing EJB DI in action in the last few chapters. It's time
we take a closer look.