Theodora Fragkouli

About Theodora Fragkouli

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.

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.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

2 Responses to "Java EE CDI Dependency Injection (@Inject) tutorial"

  1. Halil Karakose says:

    “CDI Bean scopes” link inside the article is broken: http://www.javacodegeeks.com/tutorials/cdi-bean-scopes

Leave a Reply


5 × = fifteen



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close