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