如果你沒有看到這個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.springframework.context.support.ClassPathXmlApplicationContext@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.springframework.beans.factory.support.DefaultListableBeanFactory@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>
再重新跑一次junit:結果爲:
四月 17, 2014 9:33:54 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@762efe5d: startup date [Thu Apr 17 21:33:54 CST 2014]; root of context hierarchy四月 17, 2014 9:33:54 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions信息: Loading XML bean definitions from class path resource [beans.xml]四月 17, 2014 9:33:54 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@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
即沒有了實例化的過程,這個時候只有在你需要它的時候,它纔會出現。比如你執行了:
NewPerson test = (NewPerson) ctx.getBean("newPerson"); 這個時候你的bean就實例化出來了。
那麼是不是我如果很多的bean都不想在IOC容器啓動的時候就加載,而是要beans.xml的每個bean裏都加上lazy-init屬性呢。
不用的,spring提供了default-lazy-init方法來實現這個業務。
我們只要在beans的頭裏面加上這個就ok 了
< beans default-lazy-init ="true" >
以上代碼本人親測,可用