DZone
Java Zone
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
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > CDI 2.0 for Java Is Released

CDI 2.0 for Java Is Released

After a long wait, CDI 2.0 has passed its JCP Expert Group! Get a look at the finalized features and how to start using it in your projects.

Antoine Sabot-Durand user avatar by
Antoine Sabot-Durand
·
May. 17, 17 · Java Zone · Tutorial
Like (18)
Save
Tweet
18.50K Views

Join the DZone community and get the full member experience.

Join For Free

Our JCP Expert Group is pleased to announce the release of Contexts and Dependency Injection for Java 2.0.

Specification, reference implementation (RI), and TCK can be downloaded here.

What Is CDI?

CDI is one of the major Java EE specifications.

It was introduced with Java EE 6 in 2009, updated for Java EE 7, and now, with version 2.0, it is ready for Java EE 8 as well as Java SE or other platforms like Micropofile.

CDI defines a powerful set of complementary services that help improve the structure of application code.

  • A well-defined lifecycle for stateful objects bound to lifecycle contexts, where the set of contexts is extensible.
  • A sophisticated, typesafe dependency injection mechanism, including the ability to select dependencies at either development or deployment time, without verbose configuration.
  • Support for Java EE modularity and the Java EE component architecture. The modular structure of a Java EE application is taken into account when resolving dependencies between Java EE components.
  • Integration with the Unified Expression Language (EL), allowing any contextual object to be used directly within a JSF or JSP page.
  • The ability to decorate injected objects.
  • The ability to associate interceptors to objects via typesafe interceptor bindings.
  • An event notification model.
  • A web conversation context in addition to the three standard web contexts defined by the Java Servlets specification.
  • An SPI allowing portable extensions to integrate cleanly with the container.

Major Features Included in CDI 2.0

This CDI 2.0 includes important changes for the platform.

  • The spec was split into three parts to add the support for Java SE.
  • API to boot CDI in Java SE.
  • Observers (i.e. events) ordering.
  • Asynchronous events.
  • Configurators for major SPI elements.
  • Possibility to configure or veto observer methods.
  • Add built-in annotation literals.
  • Make possible to apply interceptor on producers.
  • Alignment of Java 8 features (streams, lambdas, repeating qualifiers)

A lot of other small features will be delivered. Refer to the coming release notes to check them all.

Start Using CDI 2.0 Today With Weld 3.0

To develop your CDI 2.0 code, just add cdi-api 2.0 to your pom.xml.

<dependency>
    <groupId>javax.enterprise</groupId>
    <artifactId>cdi-api</artifactId>
    <version>2.0-PFD</version>
</dependency>


You can then run your code on Java SE or WildFly. 

Running on Java SE With Weld SE

You can then run your code on Java SE thanks to Weld SE, just add this dependency to your project:

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


You can then, bootstrap the CDI container in your code like this

public static void main(String... args) {
    try(SeContainer container = SeContainerInitializer.newInstance().initialize()) {
        // start the container, retrieve a bean and do work with it
        MyBean myBean = container.select(MyBean.class).get();
        myBean.doWork();
    }
    // shuts down automatically after the try with resources block.
}

Running on Java EE by Patching WildFly

We also provide a patch for WildFly 10.1.0 to update Weld and thus the CDI version on JBoss WildFly.

To do so, just download and unzip WildFly 10.1.0.Final, then download the patch (don't unzip it), go to the <WILDFLY_HOME>/bin directory and patch the server with the following command:

./jboss-cli.sh --command=\
"patch apply <PATH_TO_PATCH>/wildfly-10.1.0.Final-weld-3.0.0.Final-patch.zip"


You should obtain the following result in the console:

{
    "outcome" : "success",
    "result" : {}
}


Your WildFly server is now patched to use CDI 2.0 and Weld 3.0.0.Final.

GlassFish 5.0 with CDI 2.0 support should be released in the coming weeks.

Stay tuned

We'll provide more articles on CDI 2.0's new features, so stay tuned by following us on Twitter: @cdispec.

CDI Java (programming language) Java EE

Published at DZone with permission of Antoine Sabot-Durand, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Testing Your Infrastructure as Code Using Terratest
  • Python Class Attribute: Class Attribute vs. Instance Attribute
  • Secure Proxy for HIPAA-Compliant API Analytics
  • Common Types Of Network Security Vulnerabilities In 2022

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo