
Dependency injection 1.0 for Java
This specification is provided by the javax.inject API. This is a set of packages specifying a means of getting objects in such a way as to achieve major reusability, testability, and maintainability compared to traditional approaches such as constructors, factories, and service locators. This process, known as dependency injection, is beneficial to most non-trivial applications.
With CDI, you don't instantiate the classes. CDI takes care of it. An internal management of the instances lets the developer create better features for the application. Instances are a point of work where we resolve performance problems. All this becomes simpler using a CDI engine.
Take, for example, a book service:
class BookService {
final List<Book> books;
BookService () {
books = new ArrayList(...);
}
}
Using the new construct, we are adding health to our application. A good developer knows the correct time to instantiate own objects. By using this annotation, he/she doesn't need to worry about it:
class BookService {
@Inject
final List<Book> books;
}
Weld will work to instantiate the list of books and cache the result according to its scope.
Qualifiers
A qualifier identifies an injection. Through qualifiers, we establish a custom achievement for the injected object. In this case, @Random is a qualifier:
@Random
@Inject
private Instance<Integer> randomInt;
A qualifier type is annotated through the @Qualifier annotation. Qualifiers can be used at class level, field or method. Here is a sample of a qualifier declared in a method:
@Produces
@Random
private int next() {
...
}
Here's the declaration of the qualifier:
@Target( { TYPE, METHOD, PARAMETER, FIELD })
@Retention(RUNTIME)
@Documented
@Qualifier
public @interface Random {
}
If a bean does not explicitly declare a qualifier other than @Named, the bean will take the qualifier @Default as default.
EL name resolution
EL is a specification used often, but not only in web applications. It defines expressions to call injected beans and operate with them. Through EL, you can find and execute instances by different parts of your business components or web pages.
It will be seen in detail in Chapter 14 , Writing a JSF Application.
The @Named annotation can name a bean under an EL expression so that the bean can be called through this name:
@Named("my_named_test")
public class NamedBean { ... }
Here's how to call the named bean:
@Inject
@Named("my_named_test")
private NamedBean nb;
If the @Named annotation does not specify the value member, the EL name of the class will be taken as default. So, it will be injected:
@Inject
@Named("namedBean")
private NamedBean nb;
Note that, in this case, the @Named annotation is not important because if we have only NamedBean, the CDI engine succeeds in finding it anyway. Therefore, we could simply inject the bean:
@Inject
private NamedBean nb;
Resources
A resource does not have its own life cycle and that can only be called up. A resource can be, for example, an image, a CSS file, or a Word document. In CDI, we can also inject a resource through the @Resource annotation. It is very similar to the @Inject annotation, the difference being that we can specify a URL to call it.
Here are the main configurable parameters used to call the resource:
- Lookup: The identifier URL to call the resource. Usually this is done by a Java Naming and Directory Interface (JNDI) name.
- Authentication type: The authentication type where we internally authenticate to take the resource. It can be done through application or container.
- Shareable: Indicates whether this resource can be private or public. If true, the resource can be shared in all components of the application.
Here's a resource annotation that takes a DataSource:
@Resource(lookup = "java:comp/ds1", authenticationType = APPLICATION, shareable=false)
private DataSource myData1;
DataSources will seen in detail in Chapter 3 , Persistence.