Spring Series "11" Configuration bean Initialization behavior

Source: Internet
Author: User

Add Lazy-init property to a bean: Lazy-init setting only works on beans with SCOP property singleton

1 <?XML version= "1.0" encoding= "UTF-8"?>2 <Beansxmlns= "Http://www.springframework.org/schema/beans"3 Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"4 Xmlns:context= "Http://www.springframework.org/schema/context"5 xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd6 Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context-4.1.xsd ">7 8     <BeanID= "User"class= "Com.lh.entity.User"Lazy-init= "true"></Bean>9     <BeanID= "Printinfo"class= "Com.lh.util.PrintInfo"Autowire= "ByName">Ten     </Bean> One </Beans>

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:

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:context= "Http://www.springframework.org/schema/context"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-4.1.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context /spring-context-4.1.xsd "Default-lazy-init= "true">    <BeanID= "User"class= "Com.lh.entity.User"></Bean>    <BeanID= "Printinfo"class= "Com.lh.util.PrintInfo"Autowire= "ByName">    </Bean></Beans>

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).

Spring Series "11" Configuration bean Initialization behavior

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.