Java EE CDI Dependency Injection (@Inject) tutorial (original) (raw)
In this tutorial we shall show you how to achieve Dependency Injection in CDI managed Beans. In particular, we shall make use of the @Inject
annotation provided by the CDI API to inject a CDI bean to another bean. In this way the bean can be used in an application, such as a JavaServer Faces application.
CDI provides several ways to inject a bean to an application. We can inject a CDI bean, using the Field Dependency Injection, the Constructor Dependency Injection, or the Dependency Injection through the setter
method. We can also inject arguments of producer methods, but this is out of the scope of this tutorial. Let’s examine all the injection ways provided by CDI:
1. Field Dependency Injection
The easiest way to inject a CDI bean is to add the @Inject
annotation in the property to be injected. Let’s take a look at the example below. The GreetingBean
has an @Inject
annotated field, which is the helloBean
. In this way another bean, the HelloBean
is injected into the GreetingBean
.
package com.javacodegeeks.snippets.enterprise.cdibeans;
import javax.inject.Inject;
public class GreetingBean {
@Inject
private HelloBean helloBean;
}
2. Constructor Dependency Injection
When a CDI bean is initialized, the container will use its default constructor. When there is another constructor annotated with the @Inject
annotation, then the container will automatically use this constructor instead, and in this way the argument passed in the constructor will be injected in the bean. A thing to notice here is that we can only have one constructor injection point. If we create another @Inject
annotated constructor, the behaviour of the container is unpredictable.
In the code snippet below the GreetingBean
has a constructor annotated with the @Inject
annotation and with an argument, that is another bean, HelloBean
. In this way the injection is achieved.
package com.javacodegeeks.snippets.enterprise.cdibeans;
import javax.inject.Inject;
public class GreetingBean {
private final HelloBean helloBean;
@Inject
public GreetingBean(HelloBean helloBean){
this.helloBean = helloBean;
}
}
3. Dependency Injection through the setter method
In the example below, the setHelloBean(HelloBean helloBean)
method is annotated with the @Inject
annotation. Thus, when the GreetingBean
is initialized by the container the method will be invoked since it is @Inject
annotated and the HelloBean
will be injected.
package com.javacodegeeks.snippets.enterprise.cdibeans;
import javax.inject.Inject;
public class GreetingBean {
private HelloBean helloBean;
@Inject
public void setHelloBean(HelloBean helloBean) {
this.helloBean = helloBean;
}
}
4. Use of the @Any qualifier
The @Any
qualifier can be used when we have multiple implementations of one interface and we want to inject them all in another bean. Using this annotation the container will inject all implementations of the specified interface. It is used with the javax.enterprise.inject.Instance
interface provided by the CDI API, as shown in the code snippet below:
package com.javacodegeeks.snippets.enterprise.cdibeans;
import javax.enterprise.inject.Any; import javax.enterprise.inject.Instance; import javax.inject.Inject;
public class GreetingBean {
@Inject
public void getAllBeanImplementations(@Any Instance<HelloBean> beans) {
for (HelloBean helloBean : beans) {
System.out.println(helloBean.getClass().getCanonicalName());
}
}
}
Here we must notice that if there are multiple dependencies that satisfy an injection point and we will not make use of the @Any
qualifier, but try to inject one instead, then the container will fail.
5. Use of proxies to implement an injection
In order to inject a managed bean into another bean, (apart form the @Dependent
annotated beans) the CDI container will not pass a reference to the injected bean itself but will instead pass a reference to a proxy. The proxy handles all calls to the injected bean transparently. For example, when we inject a SessionScoped
bean to an ApplicationScoped
bean and many clients access the ApplicationScopedBean
, then the proxies are used to handle the calls to the injected beans. Each proxy is able to redirect the call to the correct bean.
Finally, note that a CDI proxy is created by extending the bean class and overriding all non pivate methods. Primitive types cannot be injected. The bean class must have a non-private default constructor and must not be final nor have final methods.
This was a tutorial of Dependency Injection in CDI managed Beans, using the @Inject
annotation.
Theodora has graduated from Computer Engineering and Informatics Department in the University of Patras. She also holds a Master degree in Economics from the National and Technical University of Athens. During her studies she has been involved with a large number of projects ranging from programming and software engineering to telecommunications, hardware design and analysis. She works as a junior Software Engineer in the telecommunications sector where she is mainly involved with projects based on Java and Big Data technologies.