Scope and life cycle of Spring beans

Source: Internet
Author: User

The difference between request and session request introduction

The request range is smaller, but only one.

The life cycle of the request object is a request for a client (say the exact point is a browser application), and when the request is complete, the content inside the request will be freed.

Simply put is one of your actions on the page, Request.getparameter () is to get the parameters from the URL in the previous page, form.

However, if a request involves more than one class, the following parameters are taken, which can be used Request.setattribute () and Request.getattribute ().

But when the result is output, the request ends.

Session Introduction

The session can span many pages.
The session's life cycle is also for a client, but it is in the conversation period set by others (typically 20-30 minutes), the contents of the session will always exist, even if the client browser closed the session is not necessarily released immediately.

It is understandable that multiple requests are made by the client in the same IE window.

This can be passed between parameters, such as a lot of Web site user login is used.

Difference between the two

The request takes up less resources and is more secure, but is relatively lacking in durability.

The session is relatively resource-intensive, and the security is slightly lower, but it can be implemented such as session-tracking technology.

If you can use request, try to use request because the resource consumption is more important relative to the server.

The request is passed to the next page during the delivery of the page and can no longer be passed, and Sesison is not, that is, request is limited to 2 contiguous pages

Each time a link on a webpage is a new request, when the server returns a response to the browser, the request ends, and the object saved in the request does not exist.

But when you use a browser to connect to the server, Application-server will open a session to you, when the connection time-out or when the viewer closes the session is destroyed.

So the scope of the function is not the same, the session can also track the user's state.

Ii. Scope of Spring beans

Spring 3 defines the scope for the Bean in 5, Singleton (Singleton), prototype (prototype), request, session, and global session,5 scopes, which are described below:

1.singleton

Singleton mode, there will only be one shared bean instance in the Spring IOC container, no matter how many beans reference it, always point to the same object. The singleton scope is the default scope in spring, or it can be shown to define the bean as Singleton mode, configured as:

<bean id="userDao" class="com.stonegeek.UserDaoImpl" scope="singleton"/>
2.prototype

Prototype mode, each time a prototype-defined bean is fetched through the spring container, the container creates a new instance of the bean, each with its own properties and state, and the singleton globally with only one object. Based on experience, prototype scopes are used for stateful beans, and singleton scopes are used for stateless beans.

<bean id="userDao" class="com.stonegeek.UserDaoImpl" scope="prototype"/>
3.request

For each HTTP request, the spring container creates a completely new instance based on the Bean's definition, and the instance is valid only within the current HTTP request, and other requests cannot see the state change in the current request, and the bean instance will be destroyed when the current HTTP request ends.

第一步:在web应用的xml文件中声明ContextListener<web-app> ...<listener><listener-class>org.springframework.web.context.request.RequestContextListener</listener-class></listener> ...</web-app>第二步:配置scope即可<bean id="loginAction" class="com.stonegeek.Login" scope="request"/>
4.session

As with the HTTP request, each session request creates a new instance, and the attribute is not shared between the different instances, and the instance is only valid within its own session request, and the instance is destroyed if the request ends.

省略web.xml中的声明<bean id="userPreference" class="com.stonegeek.UserPreference" scope="session"/>  
5.global Session

Relative to the session, this session is global, in a global HTTP session, the container returns the same instance of the bean, only valid when the portlet context is used.

省略web.xml中的声明<bean id="userPreference" class="com.stonegeek.UserPreference" scope="global session"/>  
Third, the life cycle and initialization process of Spring bean

The main role of the two phases of instantiation and initialization of the spring Bean is distinguished first:

Instantiation:
The instantiation process is a process of creating a bean, invoking the bean's constructor, and a singleton bean in a singleton pool

Initialization
The process of initialization is an assignment, that is, invoking the bean's setter, setting the Bean's properties

Execution process

1. (instantiate) The constructor that executes the bean

2. Inject attributes into the bean

    • Call the Setbeanname method of Beannameaware
    • Call the Setbeanfactory method of Beanfactoryaware

3. (Initialize)

    • Call the Afterpropertiesset method of Initializingbean
    • Invokes the initialization method specified by its own Init-method property

4. Perform a normal call

5. Destruction

    • Call the Destory method of Diposiblebean
    • Call the Destroy-method property to specify the destruction method
1.BeanPostProcessor interface (Spring's rear processor)

If we want to complete the bean instantiation, configuration, and other initialization methods before and after the spring container, add some of our own logic processing. We need to define one or more Beanpostprocessor interface Implementation classes, and then register them in the Spring IOC container.

Of course, there are two methods for this interface Postprocessbeforeinitialization method ,Postprocessafterinitialization method is mainly used before and after initialization,

2.InstantiationAwareBeanPostProcessor interface

The Instantiationawarebeanpostprocessor interface inherits the Beanpostprocessor interface, which provides 3 methods internally, plus 2 methods inside the Beanpostprocessor interface, So the implementation of this interface requires 5 methods. The main role of the Instantiationawarebeanpostprocessor interface is in the process of instantiating the target object, including the pre-and post-process of instantiating the object, and the property settings of the instance.

3.BeanFactoryPostProcessor interface

The Spring IOC container allows Beanfactorypostprocessor to read the bean's definition (configuration metadata) before the container instantiates any beans, and can modify it. You can also define multiple beanfactorypostprocessor by setting the ' order ' property to determine the order of each beanfactorypostprocessor execution.

Registering a Beanfactorypostprocessor instance requires defining a Java class to implement the Beanfactorypostprocessor interface and overriding the Postprocessorbeanfactory method of the interface. The bean's definition information can be obtained through beanfactory, and the bean's definition information can be modified. This is the biggest difference from beanpostprocessor.

4.bean of its own method

This includes methods called by the bean itself and methods specified by Init-method and Destroy-method in the configuration file.

5.bean life-Cycle interface method

This includes methods for Beannameaware, Beanfactoryaware, Initializingbean, and Diposablebean these interfaces

(1) Beannameaware interface

Just one method Setbeanname (String name)

Set the name of the bean

(2) Beanfactoryaware interface

Just one way Setfactory (Beanfactory Factory)

Inject a beanfactory

(3) Initializingbean

The Initializingbean interface provides a way for the bean to initialize the method, which includes only the Afterpropertiesset method, which is the class that inherits the interface, and executes the method when the bean is initialized.

(4) Diposablebean

The Disposablebean interface only provides the Destroy () method. This method is called when the Bean is destroyed

Scope and life cycle of Spring beans

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.