spring容器的懒加载lazy-init设置
来源:互联网 发布:淘宝助理教程视频 编辑:程序博客网 时间:2024/06/05 07:34
默认情况下,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 {@Autowiredprivate 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 {@Testpublic 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>0 0
- spring容器的懒加载lazy-init设置
- spring容器的懒加载lazy-init设置
- Spring中的懒加载---lazy-init
- spring 加载过程中lazy-init 的使用与源码
- Spring的default-lazy-init 与 lazy-init
- SpringXML方式配置bean的懒加载lazy-init
- Spring--IoC容器对非lazy-init的singleton Bean实例化过程
- Spring学习08--IoC容器的高级特性(lazy-init、FactoryBean、BeanPostProcessor、autowiring)
- spring的default-lazy-init参数
- spring Bean的生命周期和lazy-init
- Spring中的lazy-init
- spring lazy-init属性
- Spring lazy-init
- spring中lazy-init
- spring的延迟初始化bean (default-lazy-init 与 lazy-init )
- spring的延迟初始化bean (default-lazy-init 与 lazy-init )
- spring容器的懒加载
- spring's lazy-init & abstract
- Quad Tiling
- linux必知会之文件操作
- 为什么csdn上传的头像是扁的捏?
- web.xml中的多个filter的运行顺序
- 数据结构-通用结构
- spring容器的懒加载lazy-init设置
- 我的软考之路(四)——数据结构与算法(2)之树与二叉树
- 7.栈的应用-四则运算算术表达式求解(算符优先法)
- Linked List Cycle
- 第五周作业--有向图邻接表表示及反向图构造
- 设计模式之解释器
- 大数相加
- delphi多线程编程
- javascrip引用类型object
原创粉丝点击热门IT博客热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 英雄塔防 防守三秒 防守 贴身防守 防守犯规 防守打法 盯人防守 外围防守 防守英文 摆脱防守 篮球防守犯规 篮球防守规则 篮球防守技巧30招 篮球人盯人防守战术 英雄岛超级防守 正当防守3手机版 篮球防守战术图解 9本靠防守冲杯阵 防寒服 防寒材料 冬季防寒帽 防寒 防寒服塞尔达 塞尔达防寒服料理怎么做 塞尔达防寒服料理 防寒保暖帐篷 防尘罩 榆木防尘 防尘快速门 usb防尘塞 衣服防尘袋 电脑防尘塞 配电柜防尘 防尘垫 防尘布 防尘网图片 防尘网厂家 防尘 煤炭防尘网 金属防尘网 防尘网规格