Scope and Lazy-init in spring Bean configuration

Source: Internet
Author: User

Spring Bean configuration defaults to Single instance

The bean defaults to Singleton.

If you do not want a singleton, the following configuration:
<bean id= "user" class= "..." singleton= "false"/>

Singleton is the configuration of this bean is a singleton, if not write, is the default value of True.

Role of scope in spring Bean configuration

The scope of the 1.Bean can be set through the scope property of the bean tag, and the scope of the bean includes:
By default scope= "Singleton", then the Bean is a singleton, and anyone getting the bean instance is the same instance;
Scope= "Prototype", any instance is a new instance;
Scope= "Request", in a Web application, the scope of each instance is the request scope;
Scope= "Session", in a Web application, each instance is scoped to the session;

Note: By default, the bean instance is instantiated when it is initialized by the spring container, and the parameterless construction method is called by default. In other cases, the bean will be instantiated when the instance is fetched.

2.Bean can specify the method that is executed after initialization by specifying the property Init-method, and the method that is executed when the Destroy-method is destroyed by specifying the property.

Syntax: <bean. destroy-method= "method name" called when destroying "init-method=" is executed after initialization/>

explanation of lazy-init in spring Bean configuration

The default behavior implemented by ApplicationContext is to instantiate all singleton beans in advance (that is, dependency injection) at startup. Early instantiation means that as part of the initialization process, the ApplicationContext instance creates and configures all the singleton beans. Usually this is a good thing, because any errors in the configuration will be discovered instantly (otherwise it may take hours or even days).

<bean id= "Testbean" class= "Com.fhx.TestBean" > The bean default setting is:

<bean id= "Testbean" class= "Com.fhx.TestBean" lazy-init= "false" > lazy-init= "false" fallback load, indicating that the spring is started and instantiated immediately.

(The Lazy-init setting only works on beans with the Scop property of Singleton)

Sometimes this default processing may not be what you want. If you do not want a singleton bean to be instantiated in advance when the ApplicationContext implementation is initialized, you can set the bean to deferred instantiation.

<bean id= "Testbean" class= "Com.fhx.TestBean" lazy-init= "true", lazy-init= "true" > Lazy loading, A bean that is set to lazy will not be instantiated in advance of ApplicationContext startup, but instead be instantiated the first time it is requested by the container through Getbean.

If a bean1, which is loaded immediately, is referenced by a deferred-loaded bean2, then Bean1 is instantiated when the container is started, and bean2 is instantiated because it is referenced by the Bean1, which also conforms to the rule that deferred-loaded beans are instantiated the first time they are invoked.

It is also possible to control lazy initialization in the container hierarchy by using the 'default-lazy-init' property on the <beans/> element. such as the following configuration:

<beans default-lazy-init= "true"><!--no beans would be eagerly pre-instantiated ...- ></beans>

If the scope property of a bean is scope= "Pototype", even if the lazy-init= "false" is set, the container starts without instantiating the bean, but instead invokes the Getbean method that is instantiated

It is also stated that:

The. Init-method property specifies the method that is executed at initialization time, and the Distory-method property specifies the method that is executed when the bean is destroyed.

Lazy-init detailed excerpt from: http://blog.csdn.net/fhx007/article/details/7016704

The original text also has the scope of the detailed http://blog.csdn.net/fhx007/article/details/7016694

Thank you, author!

Scope and Lazy-init in spring Bean configuration

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.