Java Reference
In-Depth Information
New in CDI 1.1, the container must fire an event for each bean archive, before it processes the classes that
have been packaged in that module. This event is known as the AfterTypeDiscovery event. Any observer, or class
that listens for events, of the AfterTypeDiscovery event is permitted to add and/or remove classes from the set of
alternatives, list of interceptors, or list of decorators. The beans.xml file is used to initialize values of the collections.
The AfterTypeDiscovery interface contains the methods listed in Table 7-3 .
Table 7-3. AfterTypeDiscovery Interface Methods
Method
Description
getAlternatives
Returns the set of enabled alternatives in the bean deployment archive
getAnnotatedTypes
Returns the set of enabled annotated types in the bean deployment archive
getBeansXml
Returns an InputStream that can be utilized for reading the beans.xml configuration file
for the module
getDecorators
Returns a list of enabled decorators in the bean deployment archive
getInterceptors
Returns an Iterator that can be used to traverse over the AnnotatedTypes objects that
represent the Java class or interfaces in the bean deployment archive
Priorities
The @Priority annotation has been added to CDI 1.1 to aid in the ordering of use for specific bean types. In Java EE,
the @Priority annotation can be specified on a class to indicate that the class should be invoked before another class.
In other words, the annotation allows one to indicate in what order classes should be invoked. In CDI, the @Priority
annotation can be utilized to specify the ordering of Interceptors, Alternatives, and Decorators.
Interceptors
Interceptors are part of the EJB specification, and allow developers to invoke targeted tasks outside of the business
logic of an application. Also known as cross-cutting tasks, interceptors can be bound to application events and
specified business methods so that they become invoked when a specified event occurs upon the intercepted method.
To review, interceptor methods contain one or more methods annotated with one of the following: @AroundInvoke ,
@AroundTimeout , @PostContruct , or @PreDestroy . These annotations indicate the time at which the interceptor
method or class should be invoked within the event lifecycle. In order to bind an interceptor to a class or method, an
interceptor binding type must also be declared.
The CDI 1.1 specification enhances interceptors by allowing developers to specify precedence for the invocation
of interceptors if more than one has been defined for a particular class or method. The @Priority annotation can be
specified on an interceptor in order to control interceptor invocation order. Let's take a look at an example.
Suppose that we had an interceptor named MessageInterceptor . Further suppose that it should be invoked
before any other in an application. The priority specification may look as follows:
...
@Priority(Interceptor.Priority.APPLICATION+10)
@Interceptor public class MessageInterceptor { ... }
The @Priority annotation accepts a Interceptor.Priority value. Table 7-4 lists each of the priorites that are
available for use. Each of the priority values are of type static int . According to the specification, the interceptor
containing the highest priority value is invoked first.
 
Search WWH ::




Custom Search