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 programmatic dependency disambiguation example – Injection Point inspection

In this tutorial we shall see how we can avoid programmatic dependency disambiguation when injecting Java EE CDI beans. We have already shown in the Jave EE dependency disambiguation example how to avoid dependency disambiguation in CDI beans. Here we shall show you how to avoid dependency disambiguation in a dynamic way. We will achieve this by inspecting the injection point of the bean that injects another bean’s implementation.

The programmatic disambiguation with injection point inspection will be examined by creating a simple service with two implementations. Then we will create a Producer method to produce and inject both implementations in an application.

Our preferred development environment is Eclipse. We are using Eclipse Juno (4.2) version, along with Maven Integration plugin version 3.1.0. You can download Eclipse from here and Maven Plugin for Eclipse from here. The installation of Maven plugin for Eclipse is out of the scope of this tutorial and will not be discussed. Tomcat 7 is the application server used.

Let’s begin,

1. Create a new Maven project

Go to File -> Project ->Maven -> Maven Project.


In the “Select project name and location” page of the wizard, make sure that “Create a simple project (skip archetype selection)” option is unchecked, hit “Next” to continue with default values.

new project

Here the maven archetype for creating a web application must be added. Click on “Add Archetype” and add the archetype. Set the “Archetype Group Id” variable to "org.apache.maven.archetypes", the “Archetype artifact Id” variable to "maven-archetype-webapp" and the “Archetype Version” to "1.0". Click on “OK” to continue.


In the “Enter an artifact id” page of the wizard, you can define the name and main package of your project. Set the “Group Id” variable to "com.javacodegeeks.snippets.enterprise" and the “Artifact Id” variable to "cdibeans". The aforementioned selections compose the main project package as "com.javacodegeeks.snippets.enterprise.cdibeans" and the project name as "cdibeans". Set the “Package” variable to "war", so that a war file will be created to be deployed to tomcat server. Hit “Finish” to exit the wizard and to create your project.


The Maven project structure is shown below:


    It consists of the following folders:

  • /src/main/java folder, that contains source files for the dynamic content of the application,
  • /src/test/java folder contains all source files for unit tests,
  • /src/main/resources folder contains configurations files,
  • /target folder contains the compiled and packaged deliverables,
  • /src/main/resources/webapp/WEB-INF folder contains the deployment descriptors for the Web application ,
  • the pom.xml is the project object model (POM) file. The single file that contains all project related configuration.

2. Add all the necessary dependencies

You can add the dependencies in Maven’s pom.xml file, by editing it at the “Pom.xml” page of the POM editor, as shown below:

<project xmlns="" xmlns:xsi=""
	<name>cdibeans Maven Webapp</name>


As you can see Maven manages library dependencies declaratively. A local repository is created (by default under {user_home}/.m2 folder) and all required libraries are downloaded and placed there from public repositories. Furthermore intra – library dependencies are automatically resolved and manipulated.

3. Create a simple Service a simple service that creates a greeting message for the application that uses it. It is an interface with a method that produces the greeting message.

package com.javacodegeeks.snippets.enterprise.cdibeans;

public interface GreetingCard {

	void sayHello();

The implementations of the service are shown below:

package com.javacodegeeks.snippets.enterprise.cdibeans.impl;

import com.javacodegeeks.snippets.enterprise.cdibeans.GreetingCard;

public class GreetingCardImpl implements GreetingCard {

	public void sayHello() {


package com.javacodegeeks.snippets.enterprise.cdibeans.impl;

import com.javacodegeeks.snippets.enterprise.cdibeans.GreetingCard;

public class AnotherGreetingCardImpl implements GreetingCard {

	public void sayHello() {
		System.out.println("Have a nice day!!!");


4. Create a Producer method to inject the bean

In order to inject the service to another bean, we create our own annotation. CDI allows us to create our own Java annotation, that is the, and then use it in the injection point of our application to get the correct implementation of the GreetingCard according to the GreetingType of the bean.

The Greetings is an enumeration parameterized with the implementations of the service, as shown below:

package com.javacodegeeks.snippets.enterprise.cdibeans;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import com.javacodegeeks.snippets.enterprise.cdibeans.impl.AnotherGreetingCardImpl;
import com.javacodegeeks.snippets.enterprise.cdibeans.impl.GreetingCardImpl;

@Target({ FIELD, TYPE, METHOD })
public @interface GreetingType {

	Greetings value();
	public enum Greetings {
		HELLO(GreetingCardImpl.class), HI(AnotherGreetingCardImpl.class);
		Class<? extends GreetingCard> clazz;
		private Greetings(Class<? extends GreetingCard> clazz){
			this.clazz = clazz;

		public Class<? extends GreetingCard> getClazz() {
			return clazz;

Now we can create a Producer to provide applications instances of the GreetingCard service implementations. The class is a Producer that has a method, getGreetingCard. The method takes two parameters. The first parameter is a javax.enterprise.inject.Instance parameterized with the the required bean type, that is the GreetingCard here. It is annotated with the @Any annotation that allows an injection point to refer to all beans or all events of a certain bean type. The second parameter is the javax.enterprise.inject.spi.InjectionPoint that is the field in the client application that will inject the bean using the @Inject annotation. So the method will return the correct implementation of the service according to the service type and the annotations in the injection point.

package com.javacodegeeks.snippets.enterprise.cdibeans;

import javax.enterprise.inject.Any;
import javax.enterprise.inject.Instance;
import javax.enterprise.inject.Produces;
import javax.enterprise.inject.spi.Annotated;
import javax.enterprise.inject.spi.InjectionPoint;

public class GreetingCardFactory {

	public GreetingCard getGreetingCard(@Any Instance<GreetingCard> instance, InjectionPoint ip){
		Annotated gtAnnotated = ip.getAnnotated();
		GreetingType gtAnnotation = gtAnnotated.getAnnotation(GreetingType.class);
		Class<? extends GreetingCard> greetingCard = gtAnnotation.value().getClazz();

Note that the method is annotated with an extra annotation, apart from the @Produces annotation that defines the method as Producer. The @GreetingsProducer annotation is used to the injection point to define that it makes use of the specified Producer method to inject a bean instance. It is actually a CDI Qualifier, shown below:

package com.javacodegeeks.snippets.enterprise.cdibeans;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import javax.inject.Qualifier;

@Target({ FIELD, TYPE, METHOD })
public @interface GreetingsProducer {


5. Run the application

In order to run the application, we have created a simple servlet.

In the servlet below both implementations are injected. Each injection point in the servlet is a field, where the @Inject annotation is used. It is also annotated with the @GreetingsProducer annotation to specify the Producer that will be used as also with the @GreetingType annotation that specifies which implementation will be produced by the Producer.

package com.javacodegeeks.snippets.enterprise.cdibeans.servlet;


import javax.inject.Inject;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.javacodegeeks.snippets.enterprise.cdibeans.GreetingCard;
import com.javacodegeeks.snippets.enterprise.cdibeans.GreetingType;
import com.javacodegeeks.snippets.enterprise.cdibeans.GreetingType.Greetings;
import com.javacodegeeks.snippets.enterprise.cdibeans.GreetingsProducer;

@WebServlet(name = "greetingServlet", urlPatterns = {"/sayHello"})
public class GreetingServlet extends HttpServlet {

	private static final long serialVersionUID = 2280890757609124481L;
	private GreetingCard greetingCard;

	private GreetingCard anotherGreetingCard;

	  public void init() throws ServletException {

	  public void doGet(HttpServletRequest request, HttpServletResponse response)
	            throws ServletException, IOException {
	      PrintWriter out = response.getWriter();
	      out.println("<h1>" + greetingCard.sayHello() + "</h1>");
	      out.println("<h1>" + anotherGreetingCard.sayHello() + "</h1>");
	  public void destroy(){


To run the example we must build the project with Maven, and then place the war file produced in webbaps folder of tomcat. Then, we can hit on :


and the result is the one shown below:


This was a tutorial of Java EE CDI programmatic dependency disambiguation with injection point inspection.
Download the source code of this tutorial:

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!  

Leave a Reply

3 × four =

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