CDI provides us with the ability to name our beans via the @Named
annotation. Named beans allow us to easily inject our beans into other classes that depend on them (see the next section), and to easily refer to them from JSF pages via the unified expression language.
The following example shows the @Named
annotation in action:
package net.ensode.javaee8book.cdidependencyinjection.beans; import javax.enterprise.context.RequestScoped; import javax.inject.Named; @Named @RequestScoped public class Customer { private String firstName; private String lastName; public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } }
As we can see, all we need to do to name our class is to decorate it with the @Named
annotation. By default, the...