News Ticker

Introduction to CDI (Part 2)

Get Started with Context and Dependency Injection

In part one of this introduction to CDI I introduced the concept of Context and Dependency Injection and talked about producer methods. In part two I will discuss the use of Expression Language Beans, Type Safety, Scopes and the extensibility of CDI.

Expression Language Beans

Beans that you want to make accessible to Expression Language are annotated @Named. The bean is accessed by using the bean name with the first letter in lowercase. Refer back to the article on JavaServer Faces to learn more.

Type Safety

The Dependency Injection (DI) service enables you to inject objects in a typesafe way and to choose at deployment time which implementation of a particular interface to inject. So, instead of injecting objects by a string name, CDI uses the object’s type to resolve injections. When the type is insufficient to identify what instance to inject, a custom @Qualifier annotation can be used to disambiguate concrete implementations.

Context and Scope

The Context service empowers you to bind the lifecycle and interactions of stateful components to well-defined lifecycle contexts. Injected references to the beans are contextually aware. The references always apply to the bean that is associated with the context for the thread that is making the reference.


There are eight scopes defined and the CDI service places beans in the appropriate scope as defined by the Java EE specifications. For example, an HTTP request context and its beans exist during the lifetime of an HTTP Request. The scope annotations are:

  • @ViewScoped, @FlowScoped
  • @Transaction
  • @RequestScoped, @SessionsScoped, @ConversationScoped, @ApplicationScoped, @Dependent

There are two scopes that related to JSF @ViewScoped and @FlowScoped, one scope that relates to transactions and three that relate to HTTP lifecycles, an application scope and the default, which is the dependent scope.

Custom Scopes

You can also define and implement custom scopes. Custom scopes are likely to be used by those who implement and extend the CDI specification.


One of the most powerful features of Java Enterprise Edition is the way that Enterprise Java allows extensibility.

Every CDI container can enhance its functionality by using portable “Extensions”. Portable means that those CDI extensions are vendor neutral.

This is accomplished by a well-specified Service Provider Interface which is part of the JSR-299 specification.

There is quite a vibrant community around the development of those extensions and includes projects such as DeltaSpike, Agorava, and Arquillian.



DeltaSpike consists of a number of portable CDI extensions that provide useful features for Java application developers.





Agorava deals with Social Media based on OAuth and provides a way to authenticate using a user’s Social Media account.





Arquilian is an excellent Testing framework that is well worth your time learning to use.



Aspects: Interceptors and decorators

Another very powerful and intuitive features are the interceptors and decorators.

Interceptors interpose in method invocations or life cycle events and are normally used to implement crosscutting concerns. While the decorators allow you to add functionality at run-time and most often used to add functionality to legacy code.



What Next

If you want to up your skill in CDI and Java EE then you should take my course Learning Java Enterprise Edition. In this course, I cover a wide range of the Java EE APIs. If you are really want to level up and want your career a blossom then following courses are just what you need:

Further Reading

Still hungry for knowledge? and want to learn more about Java EE then these articles will interest you:

1 Trackback / Pingback

  1. Introduction to CDI (Part 1) Boost your career with us Professional Java EE Video Training and Tutorials

Leave a Reply

%d bloggers like this: