Spring bean延迟初始化:

官网API:

By default, ApplicationContext implementations eagerly create and configure all singleton beans as part of the initialization process. Generally, this pre-instantiation is desirable, because errors in the configuration or surrounding environment are discovered immediately, as opposed to hours or even days later. When this behavior is not desirable, you can prevent pre-instantiation of a singleton bean by marking the bean definition as lazy-initialized. A lazy-initialized bean tells the IoC container to create a bean instance when it is first requested, rather than at startup.

译文:默认情况下,Spring 容器在初始化过程中会创建和配置所有单例的bean。这种提前实例化是可取的,因为配置环境错误会被立即发现而不需要过多的时间。如果不采取这种行为,可以将单例的bean标记为延迟初始化。一个延迟初始化的bean告诉Spring IoC容器去创建这个bean实例化对象当它第一次被调用时而不是在容器启动时立即创建。

在Spring Bean声明周期这篇文档中http://www.cnblogs.com/sishang/p/6575839.html

将beanLifecycle定义添加lazy-init="true"属性

  <bean id="beanLifecycle" class="com.test.spring.BeanLifecycle" init-method="init" destroy-method="close" lazy-init="true">
<property name="name" value="张三"></property>
<property name="sex" value="男"></property>
</bean>

测试:

package com.test.spring;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class T {
ApplicationContext applicationcontext=null;
@Before
public void before() {
System.out.println("》》》Spring ApplicationContext容器开始初始化了......");
applicationcontext= new ClassPathXmlApplicationContext(new String[]{"test1-service.xml"});
System.out.println("》》》Spring ApplicationContext容器初始化完毕了......");
}
@Test
public void test() {
        String [] beans=applicationcontext.getBeanDefinitionNames();
        for(String beanName:beans){
            System.out.println(beanName);
        }
//BeanLifecycle beanLifecycle =applicationcontext.getBean("beanLifecycle",BeanLifecycle.class);
}
}

测试结果:

》》》Spring ApplicationContext容器开始初始化了......
2017-03-19 16:43:58  INFO:ClassPathXmlApplicationContext-Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@18c92ff9: startup date [Sun Mar 19 16:43:58 CST 2017]; root of context hierarchy
2017-03-19 16:43:59  INFO:XmlBeanDefinitionReader-Loading XML bean definitions from class path resource [test1-service.xml]
》》》Spring ApplicationContext容器初始化完毕了......
org.springframework.context.annotation.CommonAnnotationBeanPostProcessor#0
beanLifecycle
postProcessor

***********

发现Spring IoC容器中虽然有beanLifecycle ID,但是容器并没有实例化它,因为beanLifecycle的创建信息没有被执行

**********

将测试中BeanLifecycle beanLifecycle =applicationcontext.getBean("beanLifecycle",BeanLifecycle.class)这段代码解除注释,beanLifecycle被第一次请求,容器会立即创建它。

------------------------------------------------------------------------------------------------------------------------------------------------

测试结果:

》》》Spring ApplicationContext容器开始初始化了......
2017-03-19 16:49:21  INFO:ClassPathXmlApplicationContext-Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@406c9125: startup date [Sun Mar 19 16:49:21 CST 2017]; root of context hierarchy
2017-03-19 16:49:21  INFO:XmlBeanDefinitionReader-Loading XML bean definitions from class path resource [test1-service.xml]
》》》Spring ApplicationContext容器初始化完毕了......
narCodeService
org.springframework.context.annotation.CommonAnnotationBeanPostProcessor#0
beanLifecycle
postProcessor
》》》调用无参构造方法了
》》》调用BeanLifecycle对象null属性set方法,设值为:张三
》》》调用BeanLifecycle对象null属性set方法,设值为:男
》》》调用BeanNameAware接口setBenaName方法: beanLifecycle
》》》调用BeanFactoryAware接口setBeanFactory方法:org.springframework.beans.factory.support.DefaultListableBeanFactory@5ccc995c: defining beans [narCodeService,org.springframework.context.annotation.CommonAnnotationBeanPostProcessor#0,beanLifecycle,postProcessor]; root of factory hierarchy
》》》调用ApplicationContextAware接口setApplicationContext方法:org.springframework.context.support.ClassPathXmlApplicationContext@406c9125: startup date [Sun Mar 19 16:49:21 CST 2017]; root of context hierarchy
后置处理器处理bean=【beanLifecycle】开始
》》》注解初始化方法被调用
》》》BeanLifecycle调用了InitailizingBean的afterPorpertiesSet方法了.....
》》》init方法被调用
后置处理器处理bean=【beanLifecycle】完毕!

但是有一点需要注意就是:当一个非延迟初始化单例bean需要依赖注入一个延迟初始化bean,Spring 容器会立即创建这个延迟初始化的bean。因为它要满足单例的相关性

官网API:

However, when a lazy-initialized bean is a dependency of a singleton bean that is not lazy-initialized, the ApplicationContext creates the lazy-initialized bean at startup, because it must satisfy the singleton’s dependencies. The lazy-initialized bean is injected into a singleton bean elsewhere that is not lazy-initialized.

示例:

在Spring配置文件中添加如下bean定义(基于Spring Bean声明周期这篇文档中http://www.cnblogs.com/sishang/p/6575839.html):

<bean id="test" class="com.test.spring.Test" >
<property name="beanLifecycle" ref="beanLifecycle"></property>
</bean>

新建Test类:

package com.test.spring;

public class Test {

    private  BeanLifecycle beanLifecycle;

    public BeanLifecycle getBeanLifecycle() {
return beanLifecycle;
} public void setBeanLifecycle(BeanLifecycle beanLifecycle) {
this.beanLifecycle = beanLifecycle;
} }

测试:

package com.test.spring;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class T {
ApplicationContext applicationcontext=null;
@Before
public void before() {
System.out.println("》》》Spring ApplicationContext容器开始初始化了......");
applicationcontext= new ClassPathXmlApplicationContext(new String[]{"test1-service.xml"});
System.out.println("》》》Spring ApplicationContext容器初始化完毕了......");
}
@Test
public void test() {
//BeanLifecycle beanLifecycle =applicationcontext.getBean("beanLifecycle",BeanLifecycle.class);
String [] beans=applicationcontext.getBeanDefinitionNames();
for(String beanName:beans){
System.out.println(beanName);
}
//applicationcontext.getBean(BeanLifecycle.class);
}
}

测试结果:

》》》Spring ApplicationContext容器开始初始化了......
2017-03-19 17:06:19  INFO:ClassPathXmlApplicationContext-Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@406c9125: startup date [Sun Mar 19 17:06:19 CST 2017]; root of context hierarchy
2017-03-19 17:06:19  INFO:XmlBeanDefinitionReader-Loading XML bean definitions from class path resource [test1-service.xml]
》》》调用无参构造方法了
》》》调用BeanLifecycle对象null属性set方法,设值为:张三
》》》调用BeanLifecycle对象null属性set方法,设值为:男
》》》调用BeanNameAware接口setBenaName方法: beanLifecycle
》》》调用BeanFactoryAware接口setBeanFactory方法:org.springframework.beans.factory.support.DefaultListableBeanFactory@76e9e1a0: defining beans [narCodeService,org.springframework.context.annotation.CommonAnnotationBeanPostProcessor#0,beanLifecycle,postProcessor,test]; root of factory hierarchy
》》》调用ApplicationContextAware接口setApplicationContext方法:org.springframework.context.support.ClassPathXmlApplicationContext@406c9125: startup date [Sun Mar 19 17:06:19 CST 2017]; root of context hierarchy
后置处理器处理bean=【beanLifecycle】开始
》》》注解初始化方法被调用
》》》BeanLifecycle调用了InitailizingBean的afterPorpertiesSet方法了.....
》》》init方法被调用
后置处理器处理bean=【beanLifecycle】完毕!
后置处理器处理bean=【test】开始
后置处理器处理bean=【test】完毕!
》》》Spring ApplicationContext容器初始化完毕了......
org.springframework.context.annotation.CommonAnnotationBeanPostProcessor#0
beanLifecycle
postProcessor
test

从输出信息看到:beanLifecycle被容器创建了。

Spring点滴九:Spring bean的延迟初始化的更多相关文章

  1. Spring学习(九)-----Spring bean配置继承

    在 Spring,继承是用为支持bean设置一个 bean 来分享共同的值,属性或配置. 一个子 bean 或继承的bean可以继承其父 bean 的配置,属性和一些属性.另外,子 Bean 允许覆盖 ...

  2. spring学习九 spring aop详解

    本文来自于:https://www.cnblogs.com/jingzhishen/p/4980551.html AOP(Aspect-Oriented Programming,面向方面编程),可以说 ...

  3. 玩转spring MVC(九)---Spring Data JPA

    偷个懒 在网上看有写的比较好的,直接贴个链接吧:http://***/forum/blogPost/list/7000.html 版权声明:本文为博主原创文章,未经博主允许不得转载.

  4. Spring点滴二:Spring Bean

    Spring Bean: 被称作bean的对象是构成应用程序的支柱,是由Spring Ioc容器管理.bean是一个被实例化,配置.组装并由Spring Ioc容器管理对象. 官网API:A Spri ...

  5. 【转】Spring 中三种Bean配置方式比较

    今天被问到Spring中Bean的配置方式,很尴尬,只想到了基于XML的配置方式,其他的一时想不起来了,看来Spring的内容还没有完全梳理清楚,见到一篇不错的文章,就先转过来了. 以前Java框架基 ...

  6. Spring 中三种Bean配置方式比较

    今天被问到Spring中Bean的配置方式,很尴尬,只想到了基于XML的配置方式,其他的一时想不起来了,看来Spring的内容还没有完全梳理清楚,见到一篇不错的文章,就先转过来了. 以前Java框架基 ...

  7. Spring学习(十一)-----Spring使用@Required注解依赖检查

    Spring学习(九)-----Spring依赖检查 bean 配置文件用于确定的特定类型(基本,集合或对象)的所有属性被设置.在大多数情况下,你只需要确保特定属性已经设置但不是所有属性.. 对于这种 ...

  8. spring实战三装配bean之Bean的作用域以及初始化和销毁Bean

    1.Bean的作用域 所有的spring bean默认都是单例.当容器分配一个Bean时,不论是通过装配还是调用容器的getBean()方法,它总是返回Bean的同一个实例.有时候需要每次请求时都获得 ...

  9. Spring点滴四:Spring Bean生命周期

    Spring Bean 生命周期示意图: 了解Spring的生命周期非常重要,我们可以利用Spring机制来定制Bean的实例化过程. -------------------------------- ...

随机推荐

  1. Oracle中Error while performing database login with the XXXdriver; Listener refused the connection with the following error; ORA-12505,TNS:listener does not currently know of SID given inconnect descrip

    一次连接数据库怎么也连接不上,查了多方面资料,终于找到答案,总结 首先应该保证数据库的服务启动 在myeclipse的数据库视图中点 右键->new 弹出database driver的窗口,  ...

  2. [HNOI2012]矿场搭建 BZOJ2730 点双+结论

    Description 煤矿工地可以看成是由隧道连接挖煤点组成的无向图.为安全起见,希望在工地发生事故时所有挖煤点的工人都能有一条出路逃到救援出口处.于是矿主决定在某些挖煤点设立救援出口,使得无论哪一 ...

  3. 2017-2018 Exp5 MSF基础应用 20155214

    目录 Exp5 MSF基础应用 实验内容 渗透攻击 主要思路 知识点 Exp5 MSF基础应用 本次实验本实践目标是掌握metasploit的基本应用方式,重点常用的三种攻击方式的思路. 主动攻击:m ...

  4. Lambda学习---StreamApi使用

    package com.zx; import com.zx.entity.Book; import org.junit.Test; import java.time.LocalDate; import ...

  5. openstack删除僵尸卷

    问题描述: 最近在清理openstack环境,在删除cinder云硬盘时,一直发现有两个卷在删除中. 解决方法如下: 首先我们去cinder的数据库中找到这个卷,命令为: MariaDB [(none ...

  6. DevOps架构下如何进行微服务性能测试?

    一. 微服务架构下的性能测试挑战 微服务与DevOps 微服务是实现DevOps的重要架构 微服务3S原则 DevOps核心点 微服务架构下的业务特点 亿级用户的平台 单服务业务随时扩容 服务之间存在 ...

  7. 为什么说LAXCUS颠覆了我的大数据使用体验

    切入正题前,先做个自我介绍. 本人是从业三年的大数据小码农一枚,在帝都一家有点名气的广告公司工作,同时兼着大数据管理员的职责. 平时主要的工作是配合业务部门,做各种广告大数据计算分析工作,然后制成各种 ...

  8. 二叉树 c++

    树 非空树 有一个(root)根节点r 其余节点可分为m个互不相交的有限集(子树)T1....Tm 具有n个节点的树,具有(n-1)条连接(指针域),需要构成结构体,尽可能减少空间域的浪费,使用儿子兄 ...

  9. 在Mac系统下配置PHP运行环境

    概述 Mac系统对于PHP运行非常友好,我们只需要进行简单的配置便可以开始进行使用,本篇文章将一步一步地介绍Apache.PHP和MySQL的安装与配置,为开始进行开发铺好路 Apache 启动Apa ...

  10. Beta任务项录入

    今天PM把任务项整理写入TFS中,明天开始正式开发工作: