Java Reference
In-Depth Information
@Inject
works a bit differently and requires more work to narrow down which imple-
mentation of
BidService
to inject.
@Inject
requires the creation of a producer class,
which follows the factory pattern:
public class BidServiceProducer {
@Produces
@EJB(beanName="defaultBids")
@DefaultBids
BidService defaultBids;
@Produces
@EJB(beanName="clearanceBids")
@ClearanceBids
BidService clearanceBids;
}
@Inject
can now be paired with the qualifiers used in
BidServiceProducer
to in-
ject the right instance of the
BidService
EJB:
@Inject @ClearanceBids
BidService clearanceBidService;
Ultimately, to get an EJB beyond the most simple cases, even CDI needs to rely on the
@EJB
annotation in its producer classes so
@Inject
will get the right instances of the
EJBs.
5.3. AOP in the EJB world: interceptors
Have you ever been in a situation where your requirements changed toward the end of the
project and you were asked to add some common missing feature to the EJBs in your ap-
plication, such as logging or auditing? Adding logging code in each of your EJB classes
would be time consuming, and this common type of code also causes maintainability issues
and requires you to modify a number of Java classes. Well, EJB 3 interceptors solve this
problem. In this section, we'll demonstrate how to create a simple logging interceptor that
does the logging. We'll also show how this interceptor can be made the default interceptor
for your application, executing any time a bean method is executed. In this section, you'll
learn how interceptors work.