【Spring】- Bean懶加載機制

Bean類型:singleton/prototype

  • singleton Bean:單例Bean,Bean在使用過程當中只實例化1次(默認)
  • prototype Bean:原型Bean, Bean每次使用都實例化新的對象

IOC容器Bean初始化

  • ①:單例Bean:IOC啓動時則實例化
  • ②:原型Bean:IOC啓動時不實例化,真正使用時才實例化

懶加載模式:只針對單例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

原型Bean和單例Bean

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

相關文章
相關標籤/搜索