Contexts and Dependency Injection 1.2 behavior changes
The Contexts and Dependency Injection (CDI) 1.2 implementation contains some behavior changes that might cause an application that was migrated from CDI 1.0 to behave differently or fail on CDI 1.2.
While CDI 1.0 is built on the Apache OpenWebBeans implementation of CDI, CDI 1.2 is built on the Weld implementation. The behavior changes introduced reflect the differences in the two implementations.
The conversation ID CID
In the CDI 1.0 implementation, the CID is globally unique. In CDI 1.2, it is unique per HTTP session. This behavior is in line with the CDI specification and is implemented by the Weld. To get a globally unique CID, the CID must be specified at conversation start by calling Conversation.begin.
Referencing schemas in the beans.xml file
The following example of a schema is referenced in the beans.xml file:
xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"If we use an invalid schema, the server gives an exception error. Set the org.jboss.weld.xml.disableValidating=true to turn off the validation of the beans.xml file and prevent the error. If the beans.xml file specifies decorators or interceptors, a valid schema must be used, otherwise the decorators and interceptors are not correctly instantiated.
Implicit bean archives
The CDI 1.2 implementation defines two different types of bean archives, explicit and implicit.
An explicit bean archive is an archive containing a beans.xml file with any of the following characteristics:
- A version number of 1.1 (or later), and with the bean-discovery-mode of all
- No version number
- An empty file
An implicit bean archive is any other archive, which contains one or more bean classes with a bean defining annotation as defined in the specification in Section 2.5.1, "Bean defining annotations", or one or more session beans. See the specification, Contexts and Dependency Injection for the Java EE platform.
When we update the schema to a CDI 1.2 implementation, to keep the bean archive as explicit the bean discovery mode must be set to all:
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd" bean-discovery-mode="all" version="1.1" </beans>An implicit bean archive discovers only beans that have a bean defining annotation.
This new type of bean archive can result in an archive that is not intended to be a CDI bean archive, but it becomes an implicit bean archive in the CDI 1.2 implementation. To stop this behavior, we can add a beans.xml file with the bean discovery mode set to none to prevent the archive from being a bean archive. As an alternative solution, set the system property, com.ibm.ws.cdi.enableImplicitBeanArchives, to false. Setting this property to false prevents the archives without beans.xml files from becoming implicit bean archives and reduces startup time. To learn how to set the system property, see Java virtual machine settings.
Lifecycle interceptors
If the following interceptor binding is declared on lifecycle interceptors, the ElementType.TYPE is only allowed Target:
@InterceptorBinding @Retention( RetentionPolicy.RUNTIME ) @Target( {ElementType.TYPE, ElementType.METHOD} ) public @interface SFCDIInterceptorBinding {}
Related:
CDI integration with JavaServer Faces Contexts and Dependency Injection (CDI) Java virtual machine settings