DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Dependency Injection in Spring
  • Introduction to Spring Data JPA, Part 3: Unidirectional One to Many Relations
  • A Systematic Approach for Java Software Upgrades
  • Java EE 6 Pet Catalog with GlassFish and MySQL

Trending

  • The Role of AI in Identity and Access Management for Organizations
  • Navigating Change Management: A Guide for Engineers
  • Analyzing Techniques to Provision Access via IDAM Models During Emergency and Disaster Response
  • Introducing Graph Concepts in Java With Eclipse JNoSQL, Part 2: Understanding Neo4j
  1. DZone
  2. Coding
  3. Languages
  4. CDI | @Default and @Inject Annotations

CDI | @Default and @Inject Annotations

By 
Altuğ Altıntaş user avatar
Altuğ Altıntaş
·
Jun. 25, 13 · Interview
Likes (0)
Comment
Save
Tweet
Share
33.4K Views

Join the DZone community and get the full member experience.

Join For Free

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

CDI application Object (computer science) Java (programming language) Annotation Java EE

Opinions expressed by DZone contributors are their own.

Related

  • Dependency Injection in Spring
  • Introduction to Spring Data JPA, Part 3: Unidirectional One to Many Relations
  • A Systematic Approach for Java Software Upgrades
  • Java EE 6 Pet Catalog with GlassFish and MySQL

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!