default:默认是false
false:创建Bean容器时立即创建bean。
true:创建Bean容器时不会创建这个bean,等我们调用getBean的时候才会创建这个Bean。
全局配置:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" default-lazy-init="true"> </beans>局部配置:
<bean id="lazyInitDemo1" class="com.fxyh.bean.LazyInitDemo1" lazy-init="default"> <property name="id" value="1"/> <property name="name" value="zhangsan"/> </bean> <bean id="lazyInitDemo2" class="com.fxyh.bean.LazyInitDemo2" lazy-init="false"> <property name="id" value="2"/> <property name="name" value="lisi"/> </bean> <bean id="lazyInitDemo3" class="com.fxyh.bean.LazyInitDemo3" lazy-init="true"> <property name="id" value="3"/> <property name="name" value="wangwu"/> </bean>如果全局配置了,局部也配置了,除了局部配置的是default,true和false全部使用局部配置,default则按全局的配置,如果全局没有配置则是默认false。
测试用例
public class LazyInitDemo1 { public LazyInitDemo1() { System.out.println("LazyInitDemo1()构造方法"); } private Integer id; private String name; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "LazyInitDemo1{" + "id=" + id + ", name='" + name + '\'' + '}'; } } public class LazyInitDemo2 { public LazyInitDemo2() { System.out.println("LazyInitDemo2()构造方法"); } private Integer id; private String name; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "LazyInitDemo2{" + "id=" + id + ", name='" + name + '\'' + '}'; } } public class LazyInitDemo3 { public LazyInitDemo3() { System.out.println("LazyInitDemo3()构造方法"); } private Integer id; private String name; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "LazyInitDemo3{" + "id=" + id + ", name='" + name + '\'' + '}'; } } public class LazyInitDemoTest { private ApplicationContext context; private LazyInitDemo1 lazyInitDemo1; private LazyInitDemo2 lazyInitDemo2; private LazyInitDemo3 lazyInitDemo3; @Before public void setUp() throws Exception { this.context = new ClassPathXmlApplicationContext("classpath*:applicationContext-lazy.xml"); this.lazyInitDemo1 = this.context.getBean(LazyInitDemo1.class);//打断点 this.lazyInitDemo2 = this.context.getBean(LazyInitDemo2.class);//打断点 this.lazyInitDemo3 = this.context.getBean(LazyInitDemo3.class);//打断点 } @Test public void test(){ System.out.println(lazyInitDemo1); System.out.println(lazyInitDemo2); System.out.println(lazyInitDemo3); } }测试的时候在测试用例getBean的地方打断点,然后dbug就能发现哪些是懒加载,哪些是get的时候才加载。
singleton:单例。每次调用getBean()获取到的Bean是同一个Bean。默认为单例。
prototype:原型。每次调用getBean()获取到的Bean是不同的Bean。
request:暂不介绍
session:暂不介绍
global session:暂不介绍
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="scopePrototype" class="com.fxyh.bean.ScopePrototype" scope="prototype" /> <bean id="scopeSingleton" class="com.fxyh.bean.ScopeSingleton"/> </beans> public class ScopeTest { private ApplicationContext context; private ScopeSingleton scopeSingleton1; private ScopeSingleton scopeSingleton2; private ScopePrototype scopePrototype1; private ScopePrototype scopePrototype2; @Before public void setUp() throws Exception { this.context = new ClassPathXmlApplicationContext("classpath*:applicationContext-scope.xml"); this.scopeSingleton1 = this.context.getBean(ScopeSingleton.class); this.scopeSingleton2 = this.context.getBean(ScopeSingleton.class); this.scopePrototype1 = this.context.getBean(ScopePrototype.class); this.scopePrototype2 = this.context.getBean(ScopePrototype.class); } @Test public void test(){ System.out.println(scopeSingleton1 == scopeSingleton2);//true System.out.println(scopePrototype1 == scopePrototype2);//false } }