Java EE CDI Dependency Injection (@Inject) tutorial
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.
“CDI Bean scopes” link inside the article is broken: http://www.javacodegeeks.com/tutorials/cdi-bean-scopes
Hello Halil,
I have corrected the links. Thanks for the info!
How the HelloBean is implemented?
I was looking here how is HelloBean annotated to be Injectable.
Wonderful tutorial, but its incomplete. How is the HelloBean implemented? where is the actual implementation man :/
It’s a woman who wrote this FYI
What happens when you have an interface (i.e. Figure) and you have many implementations (i.e. Square, Circle, and Triangle). Later, you @Inject Figure in a managed bean, how does it know which implementation choose?