Spring重点—— IOC 容器中 Bean 的生命周期
一、理解 Bean 的生命周期,对学习 Spring 的整个运行流程有极大的帮助。
二、在 IOC 容器中,Bean 的生命周期由 Spring IOC 容器进行管理。
三、在没有添加后置处理器的情况下 Bean 的生命周期
1.通过构造器或工厂方法创建 Bean 的实例
2.为 Bean 的属性设置值好对其他 Bean 的引用
3.调用 Bean 的初始化方法
4.Bean 可以使用了
5.当容器关闭时,调用 Bean 的销毁方法
*在 Bean 的声明里设置 init-method 和 destroy-method 属性,为 Bean 指定初始化和销毁方法。
例如:
/**
* @author solverpeng
* @create 2016-07-18-20:42
*/
public class Life {
private String lifeName; public void setLifeName(String lifeName) {
System.out.println("setLifeName....");
this.lifeName = lifeName;
} public Life() {
System.out.println("constructor....");
} public void initMethod() {
System.out.println("initMethod....");
} public void destroyMethod() {
System.out.println("destroyMethod....");
} public void targetMethod() {
System.out.println("targetMethod....");
} }
spring-config.xml
<bean class="com.nucsoft.spring.bean.Life" id="life" init-method="initMethod" destroy-method="destroyMethod">
<property name="lifeName" value="myLife"/>
</bean>
Test
@Test
public void test03() {
Life life = ctx.getBean(Life.class);
life.targetMethod();
}
控制台输出:
constructor....
setLifeName....
initMethod....
targetMethod....
四、Bean 后置处理器
1.Bean 后置处理器允许在调用初始化方法前后对 Bean 进行额外的处理。
2.Bean 后置处理器对 IOC 容器里的所有 Bean 实例逐一处理。
3.具体使用:需要实现 BeanPostProcessor 接口,实现 postProcessBeforeInitialization(Object bean, String beanName) 和 postProcessAfterInitialization(Object bean, String beanName) 两个方法。
分别在 初始化方法前后被调用。
五、添加 Bean 后置处理器后的 Bean 的生命周期
1.通过构造器或工厂方法创建 Bean 的实例
2.为 Bean 的属性设置值和对其他 Bean 的引用
3.将 Bean 实例传递给 bean 后置处理器的 postProcessBeforeInitialization() 方法
4.调用 Bean 的初始化方法
5.将 Bean 实例传递给 bean 后置处理器的 postProcessAfterInitialization() 方法。
6.使用 Bean
7.当容器关闭时,调用 Bean 的销毁方法。
例如:
/**
* @author solverpeng
* @create 2016-07-18-20:42
*/
public class Life {
private String lifeName; public void setLifeName(String lifeName) {
System.out.println("setLifeName....");
this.lifeName = lifeName;
} public Life() {
System.out.println("constructor....");
} public void initMethod() {
System.out.println("initMethod....");
} public void destroyMethod() {
System.out.println("destroyMethod....");
} public void targetMethod() {
System.out.println("targetMethod....");
} }
Life.java
/**
* @author solverpeng
* @create 2016-07-18-20:58
*/
public class MyBeanPostProcessor implements BeanPostProcessor{ @Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if(bean instanceof Life) {
System.out.println("life's postProcessBeforeInitialization....");
}
return bean;
} @Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if(bean instanceof Life) {
System.out.println("life's postProcessAfterInitialization....");
}
return bean;
}
}
MyBeanPostProcessor.java
<bean class="com.nucsoft.spring.processor.MyBeanPostProcessor"/> <bean class="com.nucsoft.spring.bean.Life" id="life" init-method="initMethod" destroy-method="destroyMethod">
<property name="lifeName" value="myLife"/>
</bean>
Test
@Test
public void test03() {
Life life = ctx.getBean(Life.class);
life.targetMethod();
}
控制台输出:
constructor....
setLifeName....
life's postProcessBeforeInitialization....
initMethod....
life's postProcessAfterInitialization....
targetMethod....
Spring重点—— IOC 容器中 Bean 的生命周期的更多相关文章
- Spring学习-- IOC 容器中 bean 的生命周期
Spring IOC 容器可以管理 bean 的生命周期 , Spring 允许在 bean 声明周期的特定点执行定制的任务. Spring IOC 容器对 bean 的生命周期进行管理的过程: 通过 ...
- Spring(十二):IOC容器中Bean的生命周期方法
IOC容器中Bean的生命周期方法 1)Spring IOC容器可以管理Bean的声明周期,Spring允许在Bean生命周期的特定点执行定制的任务. 2)Spring IOC容器对Bean的生命周期 ...
- spring IOC 容器中 Bean 的生命周期
IOC 容器中 Bean 的生命周期: 1.通过构造器或工厂方法创建 Bean 实例 2.为 Bean 的属性设置值和对其他 Bean 的引用 3.调用 Bean 后置处理器接口(BeanPostPr ...
- Spring IOC容器中Bean的生命周期
1.IOC容器中Bean的生命周期 构造器函数 设置属性 初始化函数(在Bean配置中 init-method) 使用Bean 结束时关闭容器(在Bean中配置destroy-method) 2.Be ...
- IOC容器中bean的生命周期
一.Bean的生命周期 Spring IOC容器可以管理Bean的生命周期,允许在Bean生命周期的特定点执行定制的任务. Spring IOC容器对Bean的生命周期进行管理的过程如下: (1).通 ...
- [原创]java WEB学习笔记101:Spring学习---Spring Bean配置:IOC容器中bean的声明周期,Bean 后置处理器
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- Spring容器中bean的生命周期以及关注spring bean对象的后置处理器:BeanPostProcessor(一个接口)
Spring IOC 容器对 Bean 的生命周期进行管理的过程: 1.通过构造器或工厂方法创建 Bean 实例 2.为 Bean 的属性设置值和对其他 Bean 的引用 3.将 Bean 实例传递给 ...
- 7 -- Spring的基本用法 -- 9...容器中Bean的生命周期
7.9 容器中Bean的生命周期 Spring可以管理singleton作用域的Bean的生命周期,Spring可以精确地知道该Bean何时被创建,何时被初始化完成.容器何时准备销毁该Bean实例. ...
- Spring 容器中 Bean 的生命周期
Spring 容器中 Bean 的生命周期 1. init-method 和 destory-method 方法 Spring 初始化 bean 或销毁 bean 时,有时需要作一些处理工作,因此 s ...
随机推荐
- c#连接SFTP上传文件
名词解释(百度百科) sftp是Secure File Transfer Protocol的缩写,安全文件传送协议.可以为传输文件提供一种安全的加密方法.sftp 与 ftp 有着几乎一样的语法和功能 ...
- burp suite 使用教程详解(外文翻译转)
Burp Suite是Web应用程序测试的最佳工具之一,其多种功能可以帮我们执行各种任务.请求的拦截和修改,扫描web应用程序漏洞,以暴力破解登陆表单,执行会话令牌等多种的随机性检查.本文将做一个Bu ...
- IOS UISearchDisplayController 点击搜索出现黑条问题解决方案
最近项目遇到一个很奇葩的问题 点击按钮启动 presentViewController 的时候出现下图效果: 代码: AddFriendViewController *addFriendVC = [[ ...
- SQLServer公历转农历函数(1900年-2049年)
ALTER FUNCTION [dbo].[f_SysGetLunar]( @solarDay DATETIME) RETURNS varchar(20 ...
- android:style.xml
<?xml version="1.0" encoding="utf-8"?> <!-- Copyright (C) 2006 The Andr ...
- 正则指引-量词demo
class Program { static void Main(string[] args) { string str = "1\"3"; var re1 = Rege ...
- 查看android app 线程信息的命令
参考:https://my.oschina.net/zhiweiofli/blog/138454 ps | grep 'joyodream' 找到 app 的pid: joyodream为包名的一部分 ...
- 自定义StyleCop规则
参考:StyleCopSDK.chm与 Byeah的 编写StyleCop自定义规则教程(一)---编写中文备注的简单校验规则 1.建立“类库”类型的C#项目 2.加入 Microsoft.Style ...
- PlayFramework 1.2.x 在Controller 中识别JSON提交
链接 http://stackoverflow.com/questions/6132892/consuming-json-in-play-framework-controller @Global pu ...
- Onmouseover被调用多次
当一个容器,如div,不包含元素时.Onmouseover只执行一次,正常.当这个div包含其他子元素的时候,这个事件就被执行了多次,今天遇到了这个问题,特此记录下,解决方案. 这个是由于onmous ...