Introduction
One of the maybe biggest new feature in EE 6 is the Dependency Injection, DI, support. Many of you are already familiar with the this concept after been using Spring, Guice or other DI framework. But now these feature are standardized in the EE 6 specification via the following Annotations:Overview
Core DI package [http://docs.oracle.com/javaee/6/api/javax/inject/package-summary.html]- javax.inject.Inject
- javax.inject.Named
- javax.inject.Qualifier
- javax.inject.Singleton
- javax.enterprise.context.ApplicationScoped
- javax.enterprise.context.SessionScoped
- javax.enterprise.context.ConversationScoped
- javax.enterprise.context.RequestScoped
- javax.enterprise.context.Dependent
- javax.enterprise.inject.Alternative
- javax.enterprise.inject.New
- javax.enterprise.inject.Any
- javax.enterprise.inject.Produces
- javax.enterprise.inject.Disposes
javax.inject.Named
The @Named annotation is used for alternative lookup id. Example@Named("cart") public class ShoppingCart {
public String getHello() { return "Hello"; }
}
And the JSF/JSP page:
<h:outputText value="#{cart.total}" />
javax.inject.Qualifier
The @Qualifier is used to qualify injection if there are multiple candidate in the classpath. Example a bean @Inject an interface but there are several implementation of that interface in the classpath.javax.enterprise.context
You might think the above scooping Annotation are only used with JSF, but in fact these Annotation can be used on any Bean, for example try to annotate a POJO with @ApplicationScope and then grab it with the @Inject annotation in a Bean.javax.enterprise.inject.Alternative
The @Alternative annotation is used for annotating a class to be ignored for @Inject lookup, and to enable must it be declared in META-INF/beans.xml. Example:public interface IFoo {
public String getHello();
}
public class FooImpl implements IFoo {
@Override public String getHello() { return "Hello"; }
}
@javax.enterprise.inject.Alternative
public class FooAlternativeImpl implements IFoo {
@Override public String getHello() { return "Alternative Hello"; }
}
META-INF/beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
<alternatives>
<class>se.msc.example.cdi.alternative.FooAlternativeImpl</class>
</alternatives>
</beans>
And now can @Alternative be used with @Inject
public class IFooService {
@Inject IFoo iFoo;
}
javax.enterprise.inject.New
The @New annotation creates a new inject instance of Object, good example is shown in EE API, http://docs.oracle.com/javaee/6/api/javax/enterprise/inject/New.html.javax.enterprise.inject.Any
“The @Any qualifier allows an injection point to refer to all beans or all events of a certain bean type.” [http://docs.oracle.com/javaee/6/api/javax/enterprise/inject/Any.html]javax.enterprise.inject.Produces
The @Produces annotation can be seen as a factory. Good example can be seen here http://code.google.com/p/jee6-cdi/wiki/DependencyInjectionAnIntroductoryTutorial_Part1#Using_simple_@Produces.javax.enterprise.inject.Disposes
And the @Disposes is the cleanup method for factory @Produces.Now I presume that you are eager to get started testing these and annotation and that I will describe in my next blog http://magnus-k-karlsson.blogspot.se/2012/07/java-ee-6-annotations-jsr-299-contexts_27.html.
No comments:
Post a Comment