CDI | @Default and @Inject Annotations

DZone 's Guide to

CDI | @Default and @Inject Annotations

· Java Zone ·
Free Resource

CDI (Context and Dependency Injection) is a complete and lightweight injection technology designed for Java EE environment. Special container objects (EJB,EntityManager), primitive data type elements and Java class/objects written by you can be easily managed and injected as well through CDI.

Every defined Java class in each application that configured in CDI standard is a candidate to become an injectable CDI object. This default behavior is provided by @Default Annotation that was installed per each Java class secretly.


There is an Car Class which has a Vehicle implementation in the above UML diagram. A random int value is produced in sayVelocity() method and there is an output to the console such as “The Car is running at the speed of X” in work() method, where X is represents a number produced randomly.

@Default // Optional  
public class Car implements Vehicle {  
    public String work() {  
        return "Car is working in "+ sayVelocity()+" kmh.";  
    public int sayVelocity(){  
        return ThreadLocalRandom.current().nextInt(20, 240) ;  

If the above Car Class is in an application activated in CDI environment, it becomes a candidate to be an object managed by CDI. So, what is implied by the activation of CDI?

First of all, necessary dependencies need to be included in the CLASSPATH for the activation of CDI environment. If you are using an application server like Glassfish, CDI can be used without any extra definition because of the existence of CDI libraries on the application server.

But if your application is a Java SE application or is running in lightweight containers such as Tomcat, Jetty; a CDI library must be added to the project.

The reference library of CDI technology is the JBOSS Weld Archetype. For this reason, if JBOSS Weld dependencies are added to the project such as the following, first phase of the CDI activation is realized.


To activate the CDI environment, a blank CDI configuration file named beans.xml must be in the application as a requirement of the standard. This file must be located on the /META-INF/beans.xml path for Java SE applications and /WEB-INF/beans.xml path for Java EE Web applications. The existence necessity of this file may sound silly initially, but the existence of beans.xml in the directories specified above can be considered as a permission given to the container in order to activate CDI environment.

Activation of CDI environment is automatically started in Java EE web applications when beans.xml file is encountered in the /WEB-INF directory. But it is not the same for a Java SE application, starting a CDI container is the developer’s job. This case can be seen clearly if you pay attention to the following Gallery Class:

public class Gallery {  
    @Inject   // Injection point  
    private Vehicle vehicle;  
    public static void main(String[] args) {  
    Weld weld = new Weld();  
    WeldContainer weldContainer = weld.initialize();  
    Gallery gallery = weldContainer.instance().select(Gallery.class).get();  
        String message= gallery.vehicle.work();  
        System.out.println("> "+message);  

WeldContainer type object reference in Gallery class access to a CDI object which represents the initiated container environment and after this point, CDI objects are accessible and can be made injection procedures through WeldContainer object.

After this point, it is used by adopting a CDI object that is a Gallery class type through the container instance. In here, a programmatic access to the Gallery object is provided. Programmatic access is required for the startup of Java SE applications (as in Spring), but you can easily access to all CDI objects in the environment also by Annotation-based injection method through the obtained Gallery CDI object, e.g. [ private @Inject Vehicle vehicle; ]

An output as the following occurs when the Gallery Class is being run;


The real content and sample code can be accessed in http://en.kodcu.com/2013/06/cdi-default-and-inject-annotations/

Hope to see you again..


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}