Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

CDI 2.0 for Java Is Released

DZone's Guide to

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.

· Java Zone
Free Resource

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

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.

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.

Download Building Reactive Microservices in Java: Asynchronous and Event-Based Application Design. Brought to you in partnership with Red Hat

Topics:
java ee 8 ,java ,cdi 2.0 ,contexts

Published at DZone with permission of Antoine Sabot-durand. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}