Java EE CDI Bean life Cycle Introduction

Source: Internet
Author: User

This blog focuses on the life cycle of the different beans provided by CDI and how they are used in the project.


Introduction

When a bean is initialized by CDI, the Bean usually has its own scope. The scope given to it usually determines the entire life cycle of the bean. The life cycle of the bean provided by CDI:

Scope Use
applicationscoped When we use a scope that is defined as an application-wide bean, that means
The entire bean will exist throughout the lifetime of the application. Once the Bean is initially
Each time the client requests the bean to be created, the application guarantees that the
An instance.
sessionscoped Sessionscoped is mainly used for the development of applications under the Web environment. When we use a
When using a CDI bean with a domain session, the life cycle of the bean instance is determined by each
Http Session.
requestscoped Requestscoped is mainly used for the development of applications under the Web environment. When we use a
When the scope is a session's CDI Bean, the life cycle of the bean instance is determined by each
An HTTP request.
conversationscoped Conversationscoped is mainly used for the development of applications under the Web environment. Like its name, conversationscoped flags a client-to-server communication. They may be
State information used to keep multiple AJAX requests or different page requests interacting with the server.

Note: The scope of a series of CDI beans is described in Java EE7, such as transactionscoped,flowscoped and viewscoped. And so on, more information referenceJava EE 7 CDI Bean scopes.


The CDI also offers two additional pseudo-scopes:

Scope Use
Singleton
Like its name, the Singleton bean marks a single instance, and if a bean is used as a Singleton it means that only one instance exists from the beginning to the end.
Dependent
The scope of the Dependent scope Bean is the same as the scope of the bean being injected. such as when a dependent is defined as a
Bean is injected into a session bean, the bean's scope and life cycle are the same as this session bean, when the session
The end of the life cycle of this bean is also cut off.


Agent

When a bean managed by a CDI container is injected into another bean, the CDI container is not an injected instance of the bean, but rather an injected proxy class. Typically, this proxy class forwards the client's corresponding request to the appropriate bean for processing.

Think of the sessionscoped bean, if a different session requests a sessionscoped session instance, these requests will be handed to the same agent for processing. When these requests access these proxied objects, the proxy object knows how to find the appropriate object instance and provide the service for the corresponding session.

Note: The pseduo-scope of the singleton is exactly the same as this proxy pattern. When a client requests a singleton bean, the CDI container injects an instance of the bean into the appropriate bean and is no longer a proxy.


sequence Session

Some beans managed by the CDI container can maintain a long life cycle, such as sessionscoped, conversationscoped, but they must implement the serialization interface. This is due to the fact that containers are releasing resources that often need to be persisted to the physical disk along with the Bean's class information, and that the container will again be able to restore the state of those beans from the physical disk when it is needed again.


Singleton pseudo-scoped

We know that when we use singlescoped, the client gets a real reference to that object instance. Therefore, when the client requests serialization, the instance must be guaranteed to be singleton. We guarantee that this singleton bean is a true single-case method as follows:

1. As the Java serialization Standard declares, this singleton implements the Writereplace () and Readresolve () methods.

2. Maintain the transient state of the reference object of the singleton, and when the container deserializes the pair, the container is guaranteed to re-inject the object's reference.


Dependent Pseudo-scope

If there is no special declaration, dependent is the default scope of the CDI. Dependent means that the injected bean has the same scope as the bean being injected. The client does not share the instance, and each client gets a new instance of the object.


View Scope

    Update: Added viewscoped in Java EE7, for more information refer to java EE 7 CDI Bean Scopes java EE CDI viewscoped example

    If you are familiar with the scope of JSF beans, You may want to know which viewscoped bean conforms to this model. CDI does not provide any viewscoped But provide conversationscoped instead. The
conversationscoped does the same thing as we do, typically requiring an AJAX request that maintains state between a viewscoped JSF bean (The Bean), and is more likely to maintain the same dialog or state of a different page request.

Note: Keep in mind that if a user leaves the current conversation without ending such a conversation, the managed bean will remain in effect until it times out. So if you have a real problem these beans are valid until time out, if you really need a bean like a JSF viewscoped Bean , you should refer to the following implementation instead:
. Apache Deltaspike (front seam face)
. Viewaccessscope beans from myfaces codi

Java EE CDI Bean life Cycle Introduction

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.