As a general rule, avoid using interceptors for injecting business logic or changing the execution
behavior. Interceptors are great when you need repetitive work that covers some methods or classes.
AOP is a popular subject that has many supporters but also many enemies. As expected, it is not
a panacea that solves all problems. Aspects can greatly reduce code readability and complicate the
overall application l ow if not used properly.
However, aspects can be magical tools that implement additional behavior to the existing code
base with minimal effort. You can easily turn them on or off depending on the running time
environment. For example, you can turn off a logging aspect during development and put it into
action in the test environment.
Java EE offers simple interceptors that support annotations and need little XML coni guration,
except in special cases. You can use interceptors both in EJB and MDB contexts either at the class
or the method levels. You can also declare interceptors at a default level, which targets all EJBs
matching the given criteria. The default level and ordering needs some coni guration to be done in
the EJB‐JAR XML i le.
CDI adds great extensibility and functionality to interceptors. You can easily customize CDI
interceptors and use them in a cleaner way with the @InterceptorBinding annotation. You can use
interceptor bindings to wrap other interceptor bindings, forming a chain of interceptors to execute.
CDI interceptors do need minimal XML coni guration to help the CDI container determine the
EJB and CDI interceptors work alone or together in harmony. They offer all the functionality
needed to implement AOP without a third‐party framework.
The proper use of interceptors creates beautifully crafted applications with magical execution l ow.
When it is time to decide to implement interceptors, make sure they don't change business l ow and
contain application logic.
1. Design Patterns: Elements of Reusable Object‐Oriented Software (Addison‐Wesley, 1994):
Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides.
2. Head First Design Patterns (O'Reilly, 2004): Eric Freeman, Elisabeth Robson, Bert Bates,