spring容器的懒加载lazy-init设置
默认情况下,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 {
- @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" >
以上代码本人亲测,可用
spring容器的懒加载lazy-init设置相关推荐
- spring容器的懒加载
默认情况下,spring的IOC容器中lazy-init是false的,即没有打开懒加载模式. 如果你没有看到这个lazy-init 的参数设置就说明是false啦. 那么什么是懒加载? 懒加载--- ...
- spring bean的懒加载原理
spring bean的懒加载原理 1 普通的bean的 初始化是在初始化阶段开始执行的,而被lazy-init修饰的bean则是从容器第一次进行context.getbean("" ...
- Spring Boot 全局懒加载
文章目录 Spring Boot 全局懒加载 1.简介 2.排除 Bean 3.Spring Boot 全局懒加载的利弊 Spring Boot 全局懒加载 1.简介 Spring Boot 在版本 ...
- swift_043(Swift 懒加载(lazy) )
懒加载的优点 懒加载(lazy load),其实是延时加载,它的优点显而易见,首先,懒加载将对象的创建延迟到了需要对象的时候,这样减少了内存开销:其次,懒加载将创建对象.相关属性设置内聚在一个&quo ...
- element tree ui 全选_element-ui Tree之懒加载叶子节点设置半选效果
本来是不太想动的... 无可奈何,看到一句话[业精于勤, 荒于嬉]便还是动手写一写加深理解的同时给以后的自己留个备份吧... element-ui Tree组件如何给具有懒加载的tree设置半选效果? ...
- SpringBoot实现懒加载@Lazy
@Lazy使用说明 一般情况下,Spring容器在启动时会创建所有的Bean对象,使用@Lazy注解可以将Bean对象的创建延迟到第一次使用Bean的时候 使用方法 1.@Lazy(value = t ...
- 天天用 Spring ,Bean 懒加载原理你还不懂吗?
普通的bean的初始化是在容器启动初始化阶段执行的,而被lazy-init修饰的bean 则是在从容器里第一次进行context.getBean("")时进行触发. Spring ...
- react 16.6 懒加载 Lazy 尝鲜
react 16.6 发布了新的功能 lazy ,和一个组件 Suspense 下面我们看一下他的用法 首先我们先创建两个组件 LazyTest.1 和 LazyTest.2,内容相同 import ...
- 懒加载Lazy Loading
"懒加载"也被叫作"延迟价值",它的核心思想是把对象的实例化延迟到真正调用该对象的时候,这样做的好处是可以减轻大量对象在实例化时对资源的消耗,而不是在程序初始化 ...
最新文章
- java中静态是什么,java中静态和非静态有什么区别
- matlab中的i=1 length,黄伟建:matlab for i=1:length(y) 什么意思
- 【推荐系统】基于MovieLens数据集实现的协同过滤算法
- 搭建keepalived遇到的问题
- websettings 哪里设置_江阴整站优化哪里好
- 教你怎么利用Matlab画散点图
- 2021年Flash被禁用后继续使用的方法
- Gazebo models / Gazebo Error [Node.cc:90] No namespace found
- Android吉他调音器,吉他调音器:GuitarTuna
- 易优CMS插件易优CMS智能改写插件
- dnf手游体验服服务器维护,地下城与勇士手游体验服更新公告
- android RatingBar基本使用介绍
- Android 自定义TabLayout
- cloudsim资料收集
- Centos 7 拨号上网
- opencv识别图像红色区域,并输出红色区域中心点坐标
- CTFshow web3 菜鸡刷题记录
- 最短移臂调度算法_MATLAB优化算法实例——蚁群算法
- Nexus-配置VDC
- C语言/第二周/第三节/表达式
热门文章
- 让linux识别html,8 款浏览器对 HTML5 支持评测
- 网络工程师软件安装包合集
- 帝国源码php安装文件是哪个,帝国CMS数据库配置文件是哪个
- BZOJ 1022 [SHOI2008]小约翰的游戏John
- [SHOI 2008]小约翰的游戏
- 视频号新人直播应该准备什么?
- 從turtle海龜動畫 學習 Python - 高中彈性課程系列 3 烏龜繪圖 所需之Python基礎
- Linux 上好用的 R 语言 IDE
- 【U8+】用友U8数据卸出提示“更改卸载业务单据关闭日期删除后处理出错,错误描述,存储过程没有任何参数”
- python 词库 匹配_python从一段文本中找出存在于词库的词语