好记性不如烂笔头86-spring3学习(7)-ApplicationContext中bean的生命周期
假设使用ApplicationContext来生成、管理Bean, 一个Bean从建立到销毁,会历经几个运行阶段。
我个人理解一般的bean的生命周期主要包含:建立,初始化,使用阶段,销毁四个核心阶段。使用ApplicationContext 和BeanFactory对bean的生命周期来看,宏观上基本同样,微观上有一些差异。
假设使用ApplicationContext来生成并管理Bean。在运行BeanfactoryAware的
setBeanFactory()阶段之后。若Bean类上有实现org.springframework.context.ApplicationContextAware接口,则运行setApplicationContext()方法,接着才继续进行BeanPostProcessors的processBeforeInitialization()及之后的流程。
在非WEB应用程序中。若想在关闭容器之前调用Bean定义文件里Singleton的Bean所设置的destroy方法。则能够运行AbstractApplicationContext的registerShutdownHook()方法,并向JVM注冊相关方法。比如:
AbstractApplicationContext context = new ClassPathXmlApplicationContext
(“beans-config.xml”);
context.registerShutdownHook();
//运行应用程序
在应用程序结束之前,Bean定义文件上所设置的destroy方法将会被调用运行。
假设使用BeanFactory,仅仅有在使用getBean()方法真正取得Bean时,才会做实例化的动作;
假设使用ApplicationContext,则会预先针对Bean定义文件的内容,将全部的Bean实例化。假设这不是你想要的,能够在上设置”lazy-init”属性为”true,ApplicationContext 就不会在启动的时候针对该Bean作实例化的动作。比如:
<bean id="some" class="org.bearfly.bean.Some lazy-init="true"></bean>
好记性不如烂笔头86-spring3学习(7)-ApplicationContext中bean的生命周期的更多相关文章
- spring BeanFactory及ApplicationContext中Bean的生命周期
spring bean 的生命周期 spring BeanFactory及ApplicationContext在读取配置文件后.实例化bean前后.设置bean的属性前后这些点都可以通过实现接口添加我 ...
- ApplicationContext中Bean的生命周期
引言 Bean应用上下文中的生命周期和在BeanFactory中生命周期类似,不同的是,如果Bean实现了org.springframework.context.ApplicationContextA ...
- spring ApplicationContext中Bean的生命周期
AbstractApplicationContext Spring的AbstractApplicationContext是ApplicationContext的抽象实现类,该抽象类的refresh方法 ...
- Spring学习-- IOC 容器中 bean 的生命周期
Spring IOC 容器可以管理 bean 的生命周期 , Spring 允许在 bean 声明周期的特定点执行定制的任务. Spring IOC 容器对 bean 的生命周期进行管理的过程: 通过 ...
- spring in action 学习笔记四:bean的生命周期
bean 的生命周期分为:一个是ApplicationContext的容器的bean的生命周期,另一个是BeanFactory容器的生命周期. 首先介绍一下:ApplicationContext的容器 ...
- 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring Bean的生命周期
Spring 容器可以管理 singleton 作用域 Bean 的生命周期,在此作用域下,Spring 能够精确地知道该 Bean 何时被创建,何时初始化完成,以及何时被销毁. 而对于 protot ...
- 【学习笔记】Spring中的BeanFactory和ApplicationContext 以及 Bean的生命周期(Y2-3-2)
BeanFactory和ApplicationContext Spring的IoC容器就是一个实现了BeanFactory接口的可实例化类. Spring提供了两种不同的容器: 一种是最基本的Bean ...
- Spring4学习回顾之路06- IOC容器中Bean的生命周期方法
SpringIOC容器可以管理Bean的生命周期,Spring允许在Bean生命周期的特定点执行特定的任务! Spring IOC容器对Bean的生命周期进行管理的过程: -通过构造器或者工厂方法创建 ...
- Spring4.0学习笔记(5) —— 管理bean的生命周期
Spring IOC 容器可以管理Bean的生命周期,Spring允许在Bean生命周期的特定点执行定制的任务 Spring IOC 容器对Bean的生命周期进行管理的过程: 1.通过构造器或工厂方法 ...
随机推荐
- CSVHelper读出乱码 解决方案
using (FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read)) using (StreamRe ...
- Mysql怎么样避免全表扫描,sql查询优化
对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引: 尝试下面的技巧以避免优化器错选了表扫描: 使用ANALYZE TABLE tbl_name为扫 ...
- shell脚本学习之ubuntu删除多余内核
#!/bin/bash #定期删除内核 #存储命令输出cmd_output=`commands` uname_output=$(uname -r) kernel_output=`dpkg --list ...
- Educational Codeforces Round 6 A. Professor GukiZ's Robot 水
A. Professor GukiZ's Robot Professor GukiZ makes a new robot. The robot are in the point with coor ...
- [Android] Android开发优化之——对界面UI的优化(1)
在Android应用开发过程中,屏幕上控件的布局代码和程序的逻辑代码通常是分开的.界面的布局代码是放在一个独立的xml文件中的,这个文件里面是树型组织的,控制着页面的布局.通常,在这个页面中会用到很多 ...
- zzulioj--1777--和尚特烦恼3——何时能下山(水题)
1777: 和尚特烦恼3--何时能下山 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 203 Solved: 111 SubmitStatusWeb ...
- MySQL循环语句之while循环测试
转自:http://www.nuoweb.com/database/7614.html MySQL有循环语句操作,while 循环.loop循环和repeat循环,目前我只测试了 while 循环,下 ...
- 关于markdown的使用
首先: https://www.cnblogs.com/jordangong/p/9804777.html 注意:提交博客时需将 Markdown 源码粘贴到编辑器中,且编辑器没有实时预览,可以上传后 ...
- 计算label
func getCGSize(size:CGSize,fontSize:CGFloat,text:String)->CGSize{ let attributes = [NSFontAttribu ...
- xxx while the managed IDbConnection interface was being used: Login failed for user xxx
Process cube的时候遇到如下错误. Errors in the high-level relational engine. The following exception occurre ...