I intended to write this article when Liberty 8.5.5.6 was released in June last year. Liberty 8.5.5.6 provided the cdi-1.2 feature together with other Java EE 7 features such as servlet-3.1, jsf-2.2, etc. The CDI 1.2 feature was based on Weld, while the CDI 1.0 feature had used OpenWebBeans.

If you have not tried moving to CDI 1.2 from CDI 1.0 on Liberty, please consider trying it. Here are some of the cool new functionalities in CDI 1.2…

1) Global Enablement of Interceptors, Decorators, and Alternatives

In CDI 1.2, the interceptors, decorators, and alternatives can by enabled globally using @Priority:

In CDI 1.2, in addition to global enablement, the interceptors can be bound to constructors.

2) Automatic Enablement of CDI for Beans

Any classes with bean-defining annotations or session beans will be discovered as CDI beans without the presence of beans.xml. If you want to disable this (and improve startup time), place the following entry in the server.xml:

<cdi12 enableImplicitBeanArchive=”false”/>

3) Easily Prevent Scanning of Classes and Packages

You can use class exclusion filters in beans.xml to prevent scanning of classes and packages:

The first exclude filter (line 4) excludes all classes in the com.acme.rest package.

The second exclude filter (line 5) includes all classes in the com.acme.faces packages, and any subpackages, but only if com.acme.MyBean is not available.

The third exclude filter (line 8) excludes all classes in the com.acme.verbose package if the system property verbosity has the value of low.

The final exclude filter (line 10) excludes the classes in the com.acme.ejb packages, and any subpackages, if the system property exclude-ejbs is set.

4) Allow Programmatic Disabling of Classes

Use the @Vetoed annotation to allow easy programmatic disabling of classes or packages. The following snippet is to veto the class MyBean from being treated as a CDI bean:

@Vetoed
public class MyBean{
...
}

If all classes under a package need to be excluded from CDI scanning, you should put @Vetoed on the package level. The following snippet prevents all classes under the package com.acme.util from being considered CDI beans:

@Vetoed
package com.acme.util

5) Great Integration With Other Java EE Components!

As mentioned in the Java EE 7 specification, the following components support CDI injections, including constructor injection and the use of interceptors. You can freely use injection on the following component classes:

Component

Classes supporting injection

Servlet

servlets, servlet filters, event listeners

JSP

tag handlers, tag library event listeners

JSF

managed classes

JAX-WS

service endpoints, handlers

JAX-RS

JAX-RS components

WebSocket

endpoints

EJB

beans

Interceptor

interceptors

Java Persistence

entity listeners

Managed Beans

managed beans

CDI

CDI-style managed beans, decorators

Java EE platform

main class (static), login callback handler

Below is an example of a Servlet class supporting the constructor injection:

6) A Number of Java EE Components Provide CDI Extensions

When using the cdi-1.2 feature together with other Java EE 7 features, such as Transaction, BeanValidation, JMS, JBatch and JSF, you will get some additional CDI extensions provided by these features, detailed below:

Transaction CDI extension: provides the @Transactional interceptor and a new CDI scope TransactionScoped