Java Reference
In-Depth Information
Click here to view code image
<interceptors>
<class>billpayment.interceptors.LoggedInterceptor</class>
</interceptors>
If an application uses more than one interceptor, the interceptors are invoked in the order
specified in the beans.xml file.
Using Decorators in CDI Applications
A decorator is a Java class that is annotated javax.decorator.Decorator and that
has a corresponding decorators element in the beans.xml file.
A decorator bean class must also have a delegate injection point, which is annotated
javax.decorator.Delegate . This injection point can be a field, a constructor
parameter, or an initializer method parameter of the decorator class.
Decorators are outwardly similar to interceptors. However, they actually perform tasks
complementary to those performed by interceptors. Interceptors perform cross-cutting
tasks associated with method invocation and with the lifecycles of beans, but cannot per-
form any business logic. Decorators, on the other hand, do perform business logic by inter-
cepting business methods of beans. This means that instead of being reusable for different
kinds of applications as are interceptors, their logic is specific to a particular application.
For example, instead of using an alternative TestCoderImpl class for the encoder
example, you could create a decorator as follows:
Click here to view code image
@Decorator
public abstract class CoderDecorator implements Coder {
@Inject
@Delegate
@Any
Coder coder;
public String codeString(String s, int tval) {
int len = s.length();
return "\"" + s + "\" becomes " + "\"" + coder.codeString(s,
tval)
+ "\", " + len + " characters in length";
}
}
Search WWH ::




Custom Search