懒加载模式:只针对单例Bean,IOC容器启动时单例Bean先不实例化,真正调用的时候才实例化spring
测试(定义4个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" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <bean id="emp" class="com.zhiwei.beanAssemble.Employer"> <property name="id" value="0"/> <property name="name" value="广州"/> </bean> <bean id="emp1" class="com.zhiwei.beanAssemble.Employer" lazy-init="true"> <property name="id" value="1"/> <property name="name" value="北京"/> </bean> <bean id="emp2" class="com.zhiwei.beanAssemble.Employer" scope="prototype"> <property name="id" value="2"/> <property name="name" value="上海"/> </bean> <bean id="emp3" class="com.zhiwei.beanAssemble.Employer" scope="prototype" lazy-init="false"> <property name="id" value="3"/> <property name="name" value="深圳"/> </bean> </beans>
测试代码:app
ApplicationContext ac=new ClassPathXmlApplicationContext( "com/zhiwei/lazy/applicationContext.xml");
控制台打印日志:测试
Found XML schema [http://www.springframework.org/schema/beans/spring-beans-2.5.xsd] in classpath: org/springframework/beans/factory/xml/spring-beans-2.5.xsd Loading bean definitions Loaded 4 bean definitions from location pattern [com/zhiwei/lazy/applicationContext.xml] Bean factory for org.springframework.context.support.ClassPathXmlApplicationContext@2471cca7: org.springframework.beans.factory.support.DefaultListableBeanFactory@3a5ed7a6: defining beans [emp,emp1,emp2,emp3]; root of factory hierarchy Unable to locate MessageSource with name 'messageSource': using default [org.springframework.context.support.DelegatingMessageSource@3043fe0e] Unable to locate ApplicationEventMulticaster with name 'applicationEventMulticaster': using default [org.springframework.context.event.SimpleApplicationEventMulticaster@3dfc5fb8] Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@3a5ed7a6: defining beans [emp,emp1,emp2,emp3]; root of factory hierarchy Creating shared instance of singleton bean 'emp' Creating instance of bean 'emp' Eagerly caching bean 'emp' to allow for resolving potential circular references Finished creating instance of bean 'emp' Unable to locate LifecycleProcessor with name 'lifecycleProcessor': using default [org.springframework.context.support.DefaultLifecycleProcessor@646007f4] Returning cached instance of singleton bean 'lifecycleProcessor' Could not find key 'spring.liveBeansView.mbeanDomain' in any property source
分析:日志显示只实例化emp这个标准的单例Bean,使用懒加载模式的单例Bean emp1,原型Bean emp2,故意去除懒加载模式的原型Bean emp3并未随着IOC容器的初始化而实例化,这就代表懒加载模式只针对单例Beanspa
ApplicationContext ac=new ClassPathXmlApplicationContext("com/zhiwei/lazy/applicationContext.xml"); System.out.println("------------------"); System.err.println(ac.getBean("emp")); System.err.println(ac.getBean("emp1")); System.err.println(ac.getBean("emp2")); System.err.println(ac.getBean("emp3")); System.out.println("------------------"); System.err.println(ac.getBean("emp")); System.err.println(ac.getBean("emp1")); System.err.println(ac.getBean("emp2")); System.err.println(ac.getBean("emp3")); System.out.println("------------------");
结果:prototype
分析:单例Bean在第一次实例化就会被IOC容器缓存,若是再次再调用则直接从缓存中读取,原型Bean则直接实例化一个新的Bean对象3d