Bean's scope and bean initialization and destruction in spring

Source: Internet
Author: User

Scope of the Bean

Specifies the scope of the bean element by specifying scope for the bean when the bean is configured

The default value for scope is Singleton, which is singleton mode, in one applicationcontext, each time the Getbean () method is called, the same bean is obtained.

If you want to make each fetch a different bean, you can specify that the value of scope is prototype, then each call to Getbean () will get a new bean

Scope can also go to request to refer to an HTTP request as a singleton

The session is a singleton in the entire session range.

Global-session is valid in the global scope

In spring, the singleton is confined to the ClassLoader, and the popular saying is that different classloader belong to different scopes.

Initializing and destroying beans

The initialization bean and the destroying bean have corresponding methods in the bean's life cycle, by implementing the Afterpropertiesset method of the interface Initializingbean to initialize the bean, realizing the destory of the interface Dispoablebean ( ) method to destroy the bean.

We can customize the method of initializing and destroying beans by specifying the bean's initialization and destruction methods by the Init-method property and the Destory-method property of the bean element in the configuration file. For example init-method= "init" destroy-method= "Destory", then the Inith and Destory methods must be implemented in our bean.

If all of our beans have the same method of initialization and destruction, then we can then specify the Default-init-method property and the Default-destory-method method in the beans element.

Bean's scope and bean initialization and destruction in spring

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.