Java Reference
In-Depth Information
logger.severe("Invalid payment option!");
}
...
}
The argument to the fire method is a PaymentEvent that contains the payload. The
fired event is then consumed by the observer methods.
Using Interceptors in CDI Applications
An interceptor is a class used to interpose in method invocations or lifecycle events that
occur in an associated target class. The interceptor performs tasks, such as logging or
auditing, that are separate from the business logic of the application and are repeated often
within an application. Such tasks are often called cross-cutting tasks. Interceptors allow
you to specify the code for these tasks in one place for easy maintenance. When intercept-
ors were first introduced to the Java EE platform, they were specific to enterprise beans.
On the Java EE 6 platform you can use them with Java EE managed objects of all kinds,
including managed beans.
For information on Java EE interceptors, see Chapter 23 , Using Java EE Interceptors .”
An interceptor class often contains a method annotated @AroundInvoke , which speci-
fies the tasks the interceptor will perform when intercepted methods are invoked. It can
also contain a method annotated @PostConstruct , @PreDestroy , @PrePassiv-
ate , or @PostActivate , to specify lifecycle callback interceptors, and a method an-
notated @AroundTimeout , to specify EJB timeout interceptors. An interceptor class
can contain more than one interceptor method, but it must have no more than one method
of each type.
Along with an interceptor, an application defines one or more interceptor binding types ,
which are annotations that associate an interceptor with target beans or methods. For
example, the billpayment example contains an interceptor binding type named
@Logged and an interceptor named LoggedInterceptor .
The interceptor binding type declaration looks something like a qualifier declaration, but
it is annotated with javax.interceptor.InterceptorBinding :
@Inherited
@InterceptorBinding
@Retention(RUNTIME)
@Target({METHOD, TYPE})
public @interface Logged {
}
Search WWH ::




Custom Search