Contexts & Dependency Injection for Java

CDI 2.0 is released

Posted by Antoine Sabot-Durand on May 15, 2017 | Comments

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

Specification, reference implementation (JBoss Weld 3.0.0.Final) and TCK can be downloaded here.

What is CDI?

CDI is one of the major Java EE specification.

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 for Java SE or other platform 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 feature 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 your pom.xml.

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

You can then run your code on Java SE or on 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 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 release in the coming weeks.

Stay tuned

We’ll provide more article on CDI 2.0 new features so stay tuned by following @cdispec twitter account.

CDI 2.0 is in public review

Posted by Antoine Sabot-Durand on Feb 01, 2017 | Comments

CDI 2.0 is now in public review status, you can now grab the PRD of the spec ot download the Javadoc.

Major features included in CDI 2.0

This CDI 2.0 includes important changes for the platform.

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

RI is also available

We also provide a pre-release for the RI and API, thus You can start testing CDI 2.0 with Weld 3.0 CR1 that you can download here.

To develop your CDI 2.0 code just switch cdi-api in your pom.xml to this version:

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

We also provide a patch for WildFly 10.1.0 to help users evaluate CDI 2.0 under a Java EE application server.

To do so just download and unzip WildFly 10.1.0.Final, then download the patch (no need to 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.CR1-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 CR1.

Happy testing!

CDI 2.0 Beta 1 is available

Posted by Antoine Sabot-Durand on Jan 05, 2017 | Comments

After more than 2 years of work CDI 2.0 is around the corner. Its feature list is now complete and Beta for RI (Weld 3.0 Beta1) is available for download. This post lists what’s included and give you some insight on the final release and what comes next

Give it a try

First, you may want to start testing the RI and discover the new API, all the resources are listed below:

  • You can browse the spec document in html or pdf

  • If you prefer Javadoc you can read it here

  • CDI 2.0 Beta API is available on Maven Central. You can also download the jar here

  • Last but not least, the reference implementation (Weld 3.0 Beta 1) is downloadable here and can be also be used from Maven

Major features included in CDI 2.0

This CDI 2.0 includes important changes for the platform.

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

Release agenda

If everything stays on track we should send the PFD (proposed final draft) with TCK and RI to the JCP soon.

If the JCP ballot is green, CDI 2.0 final could be released before end of February.

What’s next

Our release plan for CDI 2.0 has always been to deliver it before Java EE 8 to let other spec grab our new features and make the best out of them.

We are still considering to start working on CDI 2.1 to clarify or add the few features needed for Java EE 8 and beyond.

Stay tuned.

Meet CDI at Java One 2015

Posted by Antoine Sabot-Durand on Oct 23, 2015 | Comments

During Java One 2015, a lot of CDI related talks and event will happen. find most of them in this post

In this agenda you’ll find most of CDI related talk to help you prepare your schedule

Focus on EG talks

If you want to have the best of CDI overview in Java One, don’t miss these talks:

  • CDI 2.0: Whats in the Works? (monday 12:30, Cyril Magnin I): to learn about coming features in CDI 2.0

  • Apache DeltaSpike, the CDI Toolbox (monday 2:30, Cyril Magnin I): Discover Apache DeltaSpike, the heart of CDI Ecosystem

  • Advanced CDI in live coding (tuesday 8:30, Cyril Magnin II/II): learn the power of CDI portable extension to integrate your framework in CDI programming model

I’ll be available for you questions

You’ll find me at the Hackergarten Tuesday afternoon and wednesday morning to help you on CDI, answer your question or discuss the spec. You can even pass by just to say Hello aor bring me a beer, coffee ;).

Have a nice Java One Week.

Learn CDI spec with our TCK

Posted by Antoine Sabot-Durand on Oct 15, 2015 | Comments

Beyond being one of the few open source TCK around, CDI TCK is one of the easiest to get started with.

Now that we have released a new version of spec doc embedding TCK test, it’s easier than ever.

Learn what we did and how to use it.

CDI TCK for the dummies

TCK stands for Technology Compatibility Kit. It’s one of the 3 part a JSR expert group has to release (2 others being the spec and the Reference Implementation).

A TCK is a collection of tests that actively define the spec. It’s the tool that is used to determine if an implementation is a CDI implementation or not. So this piece of code is the most important and the hardest to develop and maintain. Ok, RI could seem more important, but it’s under the spotlight and is nothing without the TCK.

To make short, TCK is the code that makes CDI a specification and thus a standard.

The great guy behind our TCK is Tomas Remes and he doing an awesome job pointing holes or unclear statements in spec text or refactoring all the TCK after CDI split in 2.0-EDR1.

If you want to learn more about the TCK I suggest that you check our dedicated page.

TCK is more than what it seems

The TCK role is not restricted to the specification mold, it can also be a great way to learn the spec by code. To crown that, thanks to Arquillian, CDI TCK is very easy to understand and very expressive.

But jumping from specification to test can be a bit complicated and this exercise can discourage many of our user.

That’s why, a few months ago, we started to think about a solution to create links from the spec text to the TCK.

It seemed quite doable since the reverse link has always existed: each since TCK tests have meta data about the spec section it refers.

So, Tomas made the required upgrade to the TCK and developed a script to add tests to the spec.

Meet Spec doc with TCK assertion

Don’t worry the original specifications docs stay unchanged, we only released a new flavor for them on our download page.

Due to normalization constraint (rules duplication avoidance) we have on the spec, some statement could seem confusing when reading them.

Let’s take, for instance, the first statement of section 3.15 Unproxyable bean types:

The container uses proxies to provide certain functionality. Certain legal bean types cannot be proxied by the container:

  • classes which don’t have a non-private constructor with no parameters,

Yeah a triple negation…​

You could take the next minute to figure out the exact meaning of the sentence or you could click on the "Show TCK assertions" link and check the two tests related to this section to see the cases where this rules make the deployment fails.

Learn, test and challenge

Now that you have this nice tool in your hand, TCK will be your CDI Lab, and perhaps, you’ll come back with corner use cases not covered or new challenges for the TCK.

Go and learn!

Tag Cloud