The scope of the bean in spring and related issues with spring container initialization _spring

Source: Internet
Author: User

Scope of Bean in spring:
In the spring container, the scope of the bean is by default a single mode, Singleton, and the single case pattern can be accessed concurrently by multiple threads, if

We want to change the scope of the bean and change it to multiple patterns, that is, each time you get the same bean from the container, it's a new bean, not

is to get the same bean with two times, with the same memory address, true. The scope of the bean is set in the configuration file, and the spring container passes the

Scope= "Prototype" to set the scope of the bean to a multiple-case pattern. As follows:
<bean id= "Userdao" class= "Com.aptech.dao.impl.UserDAOImpl" scope= "prototype" ></bean>

Spring container initialization related issues:

In the spring container, when the container is started, the bean in all containers is initialized by default, but we can specify the lazy-of the bean node

Init= "true" to delay initialization of the bean, then the bean will be initialized only when the bean is fetched. Such as:
<bean id= "Userdao" class= "Com.aptech.dao.impl.UserDAOImpl" lazy-init= "true" ></bean>
If you want to apply lazy initialization to all the beans in the spring container, you can set the default-lazy-at the root node <beans/>

Init= "True" as follows:
<beans default-lazy-init= "true"/>

In spring, when the scope of the bean is the default singleton mode, the bean is initialized when the spring container is started, and if the scope of the bean is in multiple patterns, the initialization of the bean is initialized by Beanfactory when the instance of the bean is invoked. If you want to delay the initialization of a single-mode bean when the container is started, you should follow the previous method.

What to do if you want the bean to be initialized with the corresponding method of the class corresponding to the bean, such as invoking the initialization method of the class, or opening a database connection. The Init-method property is provided for us in the bean node, which is used to specify when the bean is initialized

, while a method of the corresponding class of the bean executes concurrently, the value of the property is a method in the corresponding class. As follows: <bean id= "Userdao"

class= "Com.aptech.dao.impl.UserDAOImpl" init-method= "Getconn" ></bean>
Of course, spring also provides a property if we want to execute a method of Gaibean corresponding class while Gaibean is destroyed:

Destroy-method, this method is called before the Bean is destroyed, and the value of the property is also a method of the corresponding class, such as:
<bean id= "Userdao" class= "Com.aptech.dao.impl.UserDAOImpl" destroy-method= "CloseAll" ></bean>

How to test whether the bean is initialized at the start of the container. Since most of the bean's creation is instantiated through the class constructor, output a sentence in the constructor in the bean's corresponding class, and then create a spring container to see if the output is in the constructor, if the output proves

The bean is initialized when the Sping container is started, and if there is no output ...

Think: When is the bean destroyed? Was destroyed when the spring container was closed.
So how do you close the spring container? Use an abstract class Abstractapplicationcontext, as follows:
Abstractapplicationcontext context = new Classpathxmlapplicationcontext

("Applicationcontext.xml");
Close Spring Container
Context.close ();

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.