The CDI dependent pseudo-scope is the default CDI scope for a
bean which does not explicitly declare a scope type
bean which does not explicitly declare a scope type
This
is the default scope of a CDI bean (@Named) when nothing is specified. In this case, an
object exists to serve exactly one bean and has the same life cycle as that bean;
an instance of a dependent scoped bean is not shared between different users or
different points of injection. It can also be explicitly specified by
annotating the bean with the @Dependent annotation and importing javax.enterprise.context.Dependent. This
scope is available only in CDI and is a non-contextual scope. All CDI scopes, except this one (and @Singleton), are
known as normal scopes.
A
normal scope is a scope annotated with @NormalScope. These are contextual scopes having a client proxy. Contextual scopes
implements Contextual interface which provides operations to create and destroy contextual instances of a certain type (create() and destroy()). During create() and destroy() the Contextual interface
uses the CreationalContext
operations, push() and release().Contextual
instances with a particular scope of any contextual type are obtained via Context interface
operations (e.g.get()).
A
pseudo-scope is a scope annotated with @Scope. So, dependent scope is a pseudo-scope. This
is a non-contextual scope with no client proxy. A bean annotated with @Dependent doesn't
share injected instances between multiple injection points and the injected
bean instance is bound to the lifecycle of the container newly created object.
If the bean receives a producer field/method, disposer/observer method
invocation then any instance of that bean is available for that invocation
only. Moreover, with the exception of the container lifecycle events and
observers, any instance of the bean that receives a producer field/method,
disposer/observer method invocation is available for that invocation only.
For a @Dependent bean, the
bean constructor and a method annotated with @PostConstruct will be called for each request.
Actually, they might be called multiple times during the same request, if the
bean is used in several EL expressions. Initially, there is one instance of the
bean, and this instance is reused if the bean EL name appears multiple times in
the EL expression, but is not reused in the case of another EL expression or in
the case of a re-evaluation of the same EL expression.
Check
out the below simple example (in constructor, count is initialized with 5; in @PostConstruct is
increased by 1; in increaseCount() action
method is increased with 1):
@Named
@Dependent
public class
CountBean implements Serializable {
private static final Logger LOG =
Logger.getLogger(CountBean.class.getName());
private int count;
public CountBean() {
count = 5;
LOG.log(Level.INFO, "CountBean
Constructor! Current value: {0}", count);
}
@PostConstruct
public void CountBean() {
count++;
LOG.log(Level.INFO, "CountBean
PostConstruct! Current value: {0}", count);
}
public void increaseCount() {
count++;
LOG.log(Level.INFO, "Current value:
{0}", count);
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
<h:body>
<h:form>
<h:commandButton value="Count"
action="#{countBean.increaseCount()}"/>
</h:form>
Current value: #{countBean.count}
</h:body>
Now,
if we press the Count button
multiple times we can notice that the EL is re-evaluated during the same
request:
The
complete example is available here.
CDI:
For CDI managed beans, injection is accomplished via @Inject. For example:
JSF & CDI mixed: CDI can
be injected in JSF (vice versa is not true!)
See you in the next post about JSF none scope.
Niciun comentariu :
Trimiteți un comentariu