默认情况下,spring的IOC容器中lazy-init是false的,即没有打开懒加载模式。

如果你没有看到这个lazy-init 的参数设置就说明是false啦。

那么什么是懒加载?

懒加载---就是我们在spring容器启动的是先不把所有的bean都加载到spring的容器中去,而是在当需要用的时候,才把这个对象实例化到容器中。

例如我有如下的代码:

Java代码  
  1. package com.luch.spring.demo;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import com.luch.spring.bean.Person;
  4. public class NewPerson {
  5. @Autowired
  6. private Person person;
  7. public NewPerson(){
  8. System.out.println("lazy loading...");
  9. }
  10. public void printMsg(){
  11. if(person !=null) {
  12. System.out.println(person.getName());
  13. } else {
  14. System.out.println("no person initialize!");
  15. }
  16. }
  17. public void setPerson(Person person) {
  18. //this.person = person;
  19. }
  20. }

在无惨构造器里输出一句话,然后我先不设置懒加载模式:我有一个beans.xml的配置文件:

Xml代码  
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/context
  8. http://www.springframework.org/schema/context/spring-context.xsd">
  9. <context:annotation-config/>
  10. <bean id="person" class="com.luch.spring.bean.Person">
  11. <property name="id" value="22"></property>
  12. <property name="name" value="Jack"></property>
  13. </bean>
  14. <bean id="newPerson" class="com.luch.spring.demo.NewPerson">
  15. <property name="person" ref="person"></property>
  16. </bean>
  17. </beans>

然后我用一个junit来做测试:

Java代码  
  1. package junit.test;
  2. import static org.junit.Assert.*;
  3. import org.junit.Test;
  4. import org.springframework.context.support.AbstractApplicationContext;
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;
  6. import com.luch.spring.demo.NewPerson;
  7. public class JunitTest {
  8. @Test
  9. public void printMsg(){
  10. AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("/beans.xml");
  11. //NewPerson test = (NewPerson) ctx.getBean("newPerson");
  12. //test.printMsg();
  13. }
  14. }

这个时候输出的结果为: 
四月 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代码  
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/context
  8. http://www.springframework.org/schema/context/spring-context.xsd">
  9. <context:annotation-config/>
  10. <bean id="person" class="com.luch.spring.bean.Person">
  11. <property name="id" value="22"></property>
  12. <property name="name" value="Jack"></property>
  13. </bean>
  14. <bean id="newPerson" lazy-init="true" class="com.luch.spring.demo.NewPerson">
  15. <property name="person" ref="person"></property>
  16. </bean>
  17. </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设置相关推荐

  1. spring容器的懒加载

    默认情况下,spring的IOC容器中lazy-init是false的,即没有打开懒加载模式. 如果你没有看到这个lazy-init 的参数设置就说明是false啦. 那么什么是懒加载? 懒加载--- ...

  2. spring bean的懒加载原理

    spring bean的懒加载原理 1 普通的bean的 初始化是在初始化阶段开始执行的,而被lazy-init修饰的bean则是从容器第一次进行context.getbean("" ...

  3. Spring Boot 全局懒加载

    文章目录 Spring Boot 全局懒加载 1.简介 2.排除 Bean 3.Spring Boot 全局懒加载的利弊 Spring Boot 全局懒加载 1.简介 Spring Boot 在版本 ...

  4. swift_043(Swift 懒加载(lazy) )

    懒加载的优点 懒加载(lazy load),其实是延时加载,它的优点显而易见,首先,懒加载将对象的创建延迟到了需要对象的时候,这样减少了内存开销:其次,懒加载将创建对象.相关属性设置内聚在一个&quo ...

  5. element tree ui 全选_element-ui Tree之懒加载叶子节点设置半选效果

    本来是不太想动的... 无可奈何,看到一句话[业精于勤, 荒于嬉]便还是动手写一写加深理解的同时给以后的自己留个备份吧... element-ui Tree组件如何给具有懒加载的tree设置半选效果? ...

  6. SpringBoot实现懒加载@Lazy

    @Lazy使用说明 一般情况下,Spring容器在启动时会创建所有的Bean对象,使用@Lazy注解可以将Bean对象的创建延迟到第一次使用Bean的时候 使用方法 1.@Lazy(value = t ...

  7. 天天用 Spring ,Bean 懒加载原理你还不懂吗?

    普通的bean的初始化是在容器启动初始化阶段执行的,而被lazy-init修饰的bean 则是在从容器里第一次进行context.getBean("")时进行触发. Spring ...

  8. react 16.6 懒加载 Lazy 尝鲜

    react 16.6 发布了新的功能 lazy ,和一个组件 Suspense 下面我们看一下他的用法 首先我们先创建两个组件 LazyTest.1 和 LazyTest.2,内容相同 import ...

  9. 懒加载Lazy Loading

    "懒加载"也被叫作"延迟价值",它的核心思想是把对象的实例化延迟到真正调用该对象的时候,这样做的好处是可以减轻大量对象在实例化时对资源的消耗,而不是在程序初始化 ...

最新文章

  1. java中静态是什么,java中静态和非静态有什么区别
  2. matlab中的i=1 length,黄伟建:matlab for i=1:length(y) 什么意思
  3. 【推荐系统】基于MovieLens数据集实现的协同过滤算法
  4. 搭建keepalived遇到的问题
  5. websettings 哪里设置_江阴整站优化哪里好
  6. 教你怎么利用Matlab画散点图
  7. 2021年Flash被禁用后继续使用的方法
  8. Gazebo models / Gazebo Error [Node.cc:90] No namespace found
  9. Android吉他调音器,吉他调音器:GuitarTuna
  10. 易优CMS插件易优CMS智能改写插件
  11. dnf手游体验服服务器维护,地下城与勇士手游体验服更新公告
  12. android RatingBar基本使用介绍
  13. Android 自定义TabLayout
  14. cloudsim资料收集
  15. Centos 7 拨号上网
  16. opencv识别图像红色区域,并输出红色区域中心点坐标
  17. CTFshow web3 菜鸡刷题记录
  18. 最短移臂调度算法_MATLAB优化算法实例——蚁群算法
  19. Nexus-配置VDC
  20. C语言/第二周/第三节/表达式

热门文章

  1. 让linux识别html,8 款浏览器对 HTML5 支持评测
  2. 网络工程师软件安装包合集
  3. 帝国源码php安装文件是哪个,帝国CMS数据库配置文件是哪个
  4. BZOJ 1022 [SHOI2008]小约翰的游戏John
  5. [SHOI 2008]小约翰的游戏
  6. 视频号新人直播应该准备什么?
  7. 從turtle海龜動畫 學習 Python - 高中彈性課程系列 3 烏龜繪圖 所需之Python基礎
  8. Linux 上好用的 R 语言 IDE
  9. 【U8+】用友U8数据卸出提示“更改卸载业务单据关闭日期删除后处理出错,错误描述,存储过程没有任何参数”
  10. python 词库 匹配_python从一段文本中找出存在于词库的词语