Altuğ has posted 7 posts at DZone. You can read more from them at their website. View Full User Profile

CDI | @Default and @Inject Annotations

06.25.2013
| 6657 views |
  • submit to reddit

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.

vehicle-car

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.

<dependency>    
  <groupId>org.jboss.weld.se</groupId>    
  <artifactId>weld-se</artifactId>    
  <version>1.1.10.Final</version>    
</dependency>

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;

car-is-working

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..

Published at DZone with permission of its author, Altuğ Altıntaş.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Harald Wellmann replied on Wed, 2013/06/26 - 5:22pm

CDI has a readable specification, and Weld has an excellent reference manual.

Is there any value added by this article? I do see some statements that are plainly incorrect.

Altuğ Altıntaş replied on Thu, 2013/06/27 - 7:15am in response to: Harald Wellmann

If you tell me which statements are plainly incorrect, i would correct them.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.