Java Reference
In-Depth Information
empty. The CDI scans the classes in the archive looking for any class that conforms to the bean
requirements detailed earlier and manages and injects any such class that is not annotated with
@Vetoed
. This annotation excludes that class from being managed by the container.
In some cases, it may not be desirable to allow the container to manage any conformant bean it
i nds. If you want to restrict what the CDI container considers to be a managed bean, you can dei ne
the
bean‐discovery‐mode
property in the
bean.xml
deployment descriptor. Listing 5‐10 shows a
snippet of the
bean.xml
i le that dei nes the
bean‐discovery‐mode
property as
ALL
.
LISTING 5‐10:
The bean discovery mode is set in the bean.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="
http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://xmlns.jcp.org/xml/ns/javaee
version="1.1"
bean‐discovery‐mode="all">
...
</beans>
The
bean‐discovery‐mode
property can take one of three values:
ALL
. The
ALL
property instructs the CDI container to manage all beans that it i nds in the archive. This is the
default. The
NONE
property means that the CDI container will manage no beans, and the
ANNOTATED
property makes the archive behave like an implicit archive. In this case, the container scans for
beans with annotated scope types.
,
NONE
, or
ANNOTATED
An implicit bean archive does not contain a
bean.xml
deployment descriptor. This signals to the
CDI container that the container should only manage beans with a scope. Further details about
scoped beans are discussed later in the section, “Contexts and Scope.”
The @Inject Annotation
The
@Inject
annotation and its capabilities have already been covered. Before CDI in Java EE was
introduced, each DI framework offered its own way of injecting resources. When the Java EE CDI
container was released to work alongside the EJB container,
@Inject
annotation became a unique
and abstract interface for almost all inject operations. The
@Inject
annotation lets you use any
appropriate container or DI framework referenced for the case.
Contexts and Scope
Context is the difference between EJBs and CDI containers. Each CDI bean's life cycle is bound to a
context scope. The CDI offers four different scopes:
➤
@RequestScoped
—Duration is a user's HTTP request.
➤
@SessionScoped
—Duration is a user's HTTP session.
➤
@ApplicationScoped
—State is shared across all users for the duration of the application.
➤
@ConversationScoped
—Scope duration is controlled by the developer.