Introduction to CDI (Part 1)
Get Started with Context and Dependency Injection
In this article, I will give an overview of some of the fundamental concepts and usages of the Context and Dependency Injection framework.
You will learn that all you need to use the dependency injection framework is a compliant POJO and a qualifying injection point and that any object, including Collections, can be made injectable.
You will discover how to mark a bean so that it can be used by Expression Language to bind data directly in JavaServer Faces (JSF) and you will discover how to disambiguate a bean using the @Qualifier annotation.
You will learn about the scoping of a bean’s lifecycle and how it matches with HTTP scopes and view related scopes and discover how to mark a bean so that it can be used by Expression Language to bind data directly in JavaServer Faces (JSF) and JSP views.
Ok, so let’s get started.
What is CDI?
Java EE has one of the easiest to use and most extensible dependency injection frameworks around.
By default, almost all Java objects are injectable all they need is to comply with JSR299 and they are instantiated by the container and ready for injection into any qualifying injection point that the container finds.
No special annotation is required to identify the class you want to make injectable, as is often the case with other IoC frameworks. This may include EJBs, JNDI resources, Persistence Units and Persistence Contexts, as well as any object which would have been created by a factory.
In fact, with the help of something called “producer methods” any object can be made injectable.
Imagine you have a list of objects, say Books, and you want to inject this into a library service. You can do that by annotating a method that returns a List of books with the annotation @Produces.
To mark an injection point, use the @Inject annotation and the instance produced will be injected. Its as simple as that.
You use the @Disposes annotation to identify the dispose parameter of a disposer method. A disposer method allows the application to perform customized cleanup of an object returned by a producer method or producer field.
The @Stereotype annotation is applied to a bean that incorporates other annotations. It is used in architecture to name reoccurring patterns. So if you find that you are using the same annotations repeatedly you can create a stereotype that represents all those annotations.
In part two of this introduction to CDI I discuss the use of Expression Language Beans, Type Safety, Scopes and the extensibility of CDI.
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:
- how to develop web services with JAX-RS,
- how to develop with WebSocket and
- how to become a JSON-Processing ninja.
Still hungry for knowledge? and want to learn more about Java EE then these articles will interest you:
- JAX-RS API: an API for the construction of RESTful services
- Design Patterns: Professional Java EE design patterns
- JavaServer Faces: The JSF view language
Leave a Reply