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.

Related Whitepaper:

Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions

Get ready to program in a whole new way!

Functional Programming in Java will help you quickly get on top of the new, essential Java 8 language features and the functional style that will change and improve your code. This short, targeted book will help you make the paradigm shift from the old imperative way to a less error-prone, more elegant, and concise coding style that’s also a breeze to parallelize. You’ll explore the syntax and semantics of lambda expressions, method and constructor references, and functional interfaces. You’ll design and write applications better using the new standards in Java 8 and the JDK.

Get it Now!  

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


− five = 2



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use
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.

Sign up for our Newsletter

15,153 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books