Weld 2.0.0.Beta1 has been released. Weld 2 is the reference implementation of CDI 1.1. This release implements the Public Review Draft of CDI 1.1. You can read more about the CDI draft in Pete’s blog post.
Out of the large number of changes that the draft introduces, I would highlight the following:
Global enablement and ordering of interceptors, decorators and alternatives
An often misunderstood and disliked part of CDI is enablement of interceptors, decorators and alternatives. CDI 1.0 requires each interceptor / decorator / alternative to be explicitly enabled in each bean archive where it should be used.
CDI 1.1 enhances this concept and provides a way of enabling interceptors, decorators and alternatives globally. Such globally enabled component is only defined in a single bean archive and defines a priority which determines its global ordering. Lower priority interceptors and decorators are called first. For alternatives, if there are multiple enabled alternatives matching the same injection point, the alternative with higher priority wins.
<decorators> <class priority="100">org.mycompany.myfwk.TimestampLogger</class> <class priority="200">org.mycompany.myfwk.IdentityLogger</class> </decorators>
Alterable contexts and explicit dependency destruction
CDI 1.1 adds an ability to save resources by destroying existing contextual instances. On the SPI part the AlterableContext interface is introduced which exposes the destroy() operation. If a contextual reference is accessed after the underlying instance has been destroyed, a new instance is transparently initialized by the CDI container.
Furthermore, the built-in Instance bean now exposes a destroy() operation which can be used for:
- releasing dependent instances obtained using Instance
- destroying normal-scoped instances (a shortcut to the SPI approach above)
Decoration of built-in beans
The realm of decorators has been expanded to support decoration of CDI built-in beans. The following built-in beans support decoration:
- Event
- Instance
- Conversation
- HttpServletRequest / HttpSession / ServletContext
- InjectionPoint
- Principal
- UserTransaction
The hottest candidates for new extensions are Event (event ordering) and Conversation (conversation begin/end events)
Filtering of ProcessAnnotatedType events based on annotations present
CDI 1.1 introduces the @WithAnnotations annotation. This annotation can be used on extension observer methods to more precisely specify the events an observer method is interested in.
void observeTypesWithInject(@Observes @WithAnnotations(Inject.class) ProcessAnnotatedType<?> event) { // TODO: process types }
In the example above the extension observer method is only notified of AnnotatedTypes which define the @Inject annotation. The specified annotation can appear on the type or any supertype, on any field, method or constructor declared by the type or a supertype, or on any parameter of any method or constructor declared by the type or a supertype. The annotation may be applied as a meta-annotation on any annotation considered.
This allows for both simpler implementation of CDI extensions and faster CDI containers as the additional information exposed via @WithAnnotations allows containers to perform bootstrap optimizations.
Besides implementing the specification changes, this release also fixes more than 30 bugs. We also worked on Weld’s performance. Bootstrap performance of large deployments is now significantly better and we also tuned runtime performace of CDI-bound interceptors a bit.
As always, this release is accompanied with a CDI TCK release with tests for the new CDI 1.1 features.
UPDATE: There is no ready-made AS7 distribution this time. Instead, the Weld bundle contains an installer that you can use to patch any existing JBoss AS 7.1.1.Final or better with Weld 2. Just follow the instructions in the readme file.
Give Weld 2 a try and give us feedback. Note that CDI 1.1 is not yet final so if you do not like something in CDI there’s still time to improve it!
[ Distribution (Weld, CDI TCK)] [Release notes (Weld, CDI TCK)] [ CDI 1.1 PRD Javadoc ]