Dependency Injection the JSR 330 way

Dependency Injection is not new! There have been several ways of doing DI in your Java application. The idea behind them is well understood, but the actual realization is slightly different: XML files vs Java annotation/classes etc… That can become a pretty religious discussion :-)

With the on-going progress of the “WebBeans” specification, which defines (or defined) some common Java DI API, it was clear that there is need for a more abstract way. The new “at inject” (or Dependency Injection for Java) standard, JSR 330, was born. WebBeans has been renamed to Contexts and Dependency Injection for the Java EE platform (short: CDI). CDI now also leverages the “at inject” API, specified in JSR 330. The main idea behind this new JSR was driven by Ex-Google-Fellow Bob Lee, how is responsible for the Google-Guice DI framework.

Now, using the tiny JSR 330 API is pretty simple. First you define an injection point in a class, which wants to use a concrete implementation of a common (business…) service:

// injection-point; no get/set needed...
@Inject private BusinessService service;

Now, you need to provide an implementation:

...
public class DefaultBusinessService implements BusinessService
{
...
}
...

If you have more versions of this service, you have to create a Qualifier annotation:

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import javax.inject.Qualifier;

@Qualifier
@Target( { TYPE, METHOD, FIELD })
@Retention(RUNTIME)
public @interface FancyService
{
}

You use the Qualifier to inject a more meaningful implementation of the service:

@Inject @FancyService private BusinessService fancyService;

Now the question that comes up is what implementation of this JSR should I use ?

The ultimate suggestion would be, especially when running in the Java EE world, to use an implementation of the JSR 299. There are currently three options for that:

But what if I am not in a JavaEE container? When I am writing a tiny, small Java SE application ? The good news is here that Google Guice implements the JSR-330 as well. This is not a big surprise, since one of the guys behind the JSR-330 is Bob Lee, from Google-Guice. As a matter of fact, there is a wiki page, which acts a guide on using 330 and Guice.

Not only 299 implementations or Google Guice are supporting the unified API for Java Dependency Injection. The popular Spring framework, starting with its 3.0.x version, also has support for it!

The benefit of the wide support is that you only need to learn a simple API, which can be re-used in different environments. On Java EE containers the obligatory choice is a 299 implementation, such as Apache OpenWebBeans or JBoss Weld.But outside of the “webapplication world”, you can use the API and Guice or Spring as the implementation of the standard. Sure, most JSR-299 containers run in a vanilla JavaSE environment as well.

Summary: The API is simple and easy to use. You nail yourself to no specific platform by using/learning this API. Next blogs will be more specific to JSR-299 implementation to see what that spec adss on-top of it, such is a the producer methods., which offers a nice way to expose legacy (or vanilla JDK) classes as a injectable component.

About these ads

Howdy!

Posted in apache, ejb, fun, java, jsf, spring, WebBeans
2 comments on “Dependency Injection the JSR 330 way
  1. emmanuel delgado says:

    Good post, this is exactly what was looking for since I want to try some Java SE examples using CDI but wasn’t sure who was supporting/implementing it, I’ll try Guice.

    Regards

  2. Christian says:

    @emmanuel: Guice is not a CDI implementation

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 32 other followers

%d bloggers like this: