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. 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.

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.

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,

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

GreetingCard.java 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.

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 GreetingType.java, 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:

Now we can create a Producer to provide applications instances of the GreetingCard service implementations. The GreetingCardFactory.java 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.

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:

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.

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial3>

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

and many more ....

3 comments

I seem to be having trouble running this example on WAS 8.5. It appears that GreetingType is not passed as qualifier into Producer method. And subsequently an attempt to extract annotation of GreetingType.class yields null, because (for some reason) there is no such an annotation type in injection point.

Newsletter

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.

Email address:

Join Us

With 1,240,600 monthly unique visitors and over 500 authors we are placed among the top Java related sites around. Constantly being on the lookout for partners; we encourage you to join us. So If you have a blog with unique and interesting content then you should check out our JCG partners program. You can also be a guest writer for Java Code Geeks and hone your writing skills!

Disclaimer

All trademarks and registered trademarks appearing on Examples 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. Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.