spring容器的懶載入lazy-init設定
預設情況下,spring的IOC容器中lazy-init是false的,即沒有開啟懶載入模式。
如果你沒有看到這個lazy-init 的引數設定就說明是false啦。
那麼什麼是懶載入?
懶載入---就是我們在spring容器啟動的是先不把所有的bean都載入到spring的容器中去,而是在當需要用的時候,才把這個物件例項化到容器中。
例如我有如下的程式碼:
package com.luch.spring.demo; import org.springframework.beans.factory.annotation.Autowired; import com.luch.spring.bean.Person; public class NewPerson { @Autowired private Person person; public NewPerson(){ System.out.println("lazy loading..."); } public void printMsg(){ if(person !=null) { System.out.println(person.getName()); } else { System.out.println("no person initialize!"); } } public void setPerson(Person person) { //this.person = person; } }
在無慘構造器裡輸出一句話,然後我先不設定懶載入模式:我有一個beans.xml的配置檔案:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config/> <bean id="person" class="com.luch.spring.bean.Person"> <property name="id" value="22"></property> <property name="name" value="Jack"></property> </bean> <bean id="newPerson" class="com.luch.spring.demo.NewPerson"> <property name="person" ref="person"></property> </bean> </beans>
然後我用一個junit來做測試:
package junit.test; import static org.junit.Assert.*; import org.junit.Test; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.luch.spring.demo.NewPerson; public class JunitTest { @Test public void printMsg(){ AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("/beans.xml"); //NewPerson test = (NewPerson) ctx.getBean("newPerson"); //test.printMsg(); } }
這個時候輸出的結果為:
四月 17, 2014 9:26:41 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
資訊: Refreshing org[email protected]762efe5d: startup date [Thu Apr 17 21:26:41 CST 2014]; root of context hierarchy
四月 17, 2014 9:26:42 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
資訊: Loading XML bean definitions from class path resource [beans.xml]
四月 17, 2014 9:26:42 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
資訊: Pre-instantiating singletons in org.s[email protected]77caeb3e: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,person,newPerson,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0];
root of factory hierarchy
lazy loading..
即物件被例項化了,也就是被載入到spring的容器中去了。
然後我們設定一下懶載入模式:我們beans.xml的配置檔案. lazy-init="true"即
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config/> <bean id="person" class="com.luch.spring.bean.Person"> <property name="id" value="22"></property> <property name="name" value="Jack"></property> </bean> <bean id="newPerson" lazy-init="true" class="com.luch.spring.demo.NewPerson"> <property name="person" ref="person"></property> </bean> </beans>