JavaEE CDI - Contexts and Dependency Injection

CDI (Contexts and Dependency Injection) is a standard dependency injection framework included in Java EE 6 onwards. CDI is also part of the Eclipse MicroProfile project, a framework use for development of Microservice application.

CDI Releases

CDI Version Release Year
CDI 1.0 2009
CDI 1.1 2013
CDI 1.2 2014
CDI 2.0 2017
CDI 3.0 2020
CDI 4.0 2022

New Features

  • Removal of deprecated code
    Ex - @New qualifier which was deprecated in version 1.1
  • Change in Bean discovery Mode
    CDI 4.0 bean discovery mode is by default Annotated
  • Support for Java Module system ( introduced in Java v9)
  • Introducing CDI Lite

The “beans.xml” File

First, we must place a beans.xml file in the - src/main/resources/META-INF/ folder with bean-discovery-mode set to all or annotated
Even if this file doesn't contain any specific dependency Injection directives at all, it's required for getting CDI up and running.

bean.xml

<?xml version="1.0" encoding="UTF-8"?>
<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">
</beans>

Bean can be Injected using following injection:
- Fields
- Setter
- constructor

CDI Bean Scope

There are eight, differnt scopes defined for the bean as per Java EE specification

  1. @Dependent - Default scope of bean in CDI, It is bound to the scope of bean it get injected and won't be shared
  2. @RequestScoped – bound to the HTTP request
  3. @SessionScoped – bound to the HTTP session of an user
  4. @ApplicationScoped – it is like a Singleton bean, one instance per application.
  5. @ConversationScoped – bound to a conversation context.
  6. @ViewScope - Scope related to JSF
  7. @FlowScoped - Scope related to JSF
  8. @Transaction - Transactional scope

Important CDI annotations and their Usage

  • @Inject - Inject the dependency in client class.

  • @Default - refers to default implementation. CDI, by default, annotates all the implementations of an interface with the @Default annotation.

  • @Named - If a service has multiple implementation then to remove the ambiquity we can use @Named annotation. It also make bean accessible to Expression Language.The bean is accessed by using the bean name with the first letter in lowercase
    For example:

Service Implementation:

    @Named("GifFileEditor")
    public class GifFileEditor implements ImageFileEditor

Inject the dependency as follow:

    @Inject  
    private final @Named("GifFileEditor") ImageFileEditor imageFileEditor;
  • @Qualifier - CDI supports the use of custom qualifiers for qualifying dependencies and solving ambiguous injection points. They not only bind a semantic name to a service, but they bind injection metadata too. CDI uses the object’s type to resolve injections. When the type is insufficient to identify what instance to inject, then a custom @Qualifier annotation can be used to disambiguate concrete implementations.

  • @Produces - Dynamic approach for creating a bean that is managed by CDI container.

  • @Disposes - identify the dispose parameter of a disposer method. A disposer method allows the application to perform customized cleanup of an object returned by a producer method or producer field.

@Decorator
@Priority

Comments

Popular posts from this blog

Creating simple Maven multi module project in Java

Tricky Java Questions

How to update existing CCDT file (AMQCLCHL.TAB) for successful MQueue connection