I intended to write this article when Liberty was released in June last year. Liberty 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:

public class MyInterceptor{

Read Ashley’s article to learn more about the interceptors and how to enable the interceptors globally.

In CDI 1.0, it was necessary to enable the interceptors using the following XML but the enablement applied only to the containing archive:

<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
 </ interceptors>

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:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee">
  <exclude name="com.acme.rest.*" />
   <exclude name="com.acme.faces.**">
    <if-class-not-available name="com.acme.MyBean"/>
   <exclude name="com.acme.verbose.*">
    <if-system-property name="verbosity" value="low"/>
   <exclude name="com.acme.ejb.**">
    <if-class-available name="javax.enterprise.inject.Model"/>
     <if-system-property name="exclude-ejbs"/>
  • The first exclude filter (line 4) excludes all classes in the com.acme.rest package.
  • The second exclude filter (line 5) excludes 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:

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:

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:

@WebServlet(urlPatterns = { "/CDIConstructorInjection" })
public class CDIConstructorInjectServlet extends HttpServlet {
 /** Standard serialization ID. */
 private static final long serialVersionUID = 1L;
  /** Storage for the injected constructor bean. */
  private final CDIDataBean constructorBean;
   * Constructor injection: When this servlet is instantiated,
   * inject a bean as a parameter to the servlet constructor.
   * @param cb The constructor bean which is to be set.
  public CDIConstructorInjectServlet(ConstructorBean cb) {
   this.constructorBean = cb;
   * Post-construct injection: Invoked via injection 
   * following the construction of this servlet instance.
   void start() {
    System.out.println("Post construct is called");
   * Pre-destroy: Invoked via injection preceding the  
   * destruction of this servlet instance.
  void stop() {
   System.out.println("PreDestroy is called");
  protected void doGet(HttpServletRequest request,
   HttpServletResponse response) 
   throws ServletException, IOException {
    PrintWriter writer = response.getWriter();

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
  • Bean Validation feature: provides the Validator and ValidatorFactory beans and BValBinding interceptor
  • JMS feature: provides the JMSContext bean
  • JBatch feature: provides the beans JobContext, StepContext, and the annotation BatchProperty
  • JSF feature: provides the new CDI scopes such as ViewScoped, FlowScoped, and the FlowDefinition annotation

Using the CDI 1.2 feature is easy. Just specify the following entries in the server.xml (the example configures the server to use the CDI 1.2, Servlet 3.1, and JSF 2.2 features):


Have fun!

2016-09-02: Updated section 3 to say “excludes” instead of “includes”.

Join The Discussion

Your email address will not be published. Required fields are marked *