在Spring Bean的生命周期中各方法的执行顺序
Spring 容器中的 Bean 是有生命周期的,Spring 允许在 Bean 在初始化完成后以及 Bean 销毁前执行特定的操作,常用的设定方式有以下十种:
- 通过实现 InitializingBean 接口来定制初始化之后的操作方法;
- 通过实现DisposableBean 接口来定制销毁之前的操作方法;
- 通过元素的 init-method属性指定初始化之后调用的操作方法;
- 通过元素的 destroy-method属性指定销毁之前调用的操作方法;
- 在指定方法上加上 @PostConstruct 注解来制定该方法是在初始化之后调用;
- 在指定方法上加上 @PreDestroy 注解来制定该方法是在销毁之前调用;
- 实现BeanNameAware接口设置Bean的ID或者Name;
- 实现BeanFactoryAware接口设置BeanFactory;
- 实现ApplicationContextAware接口设置ApplicationContext;
- 注册实现了BeanPostProcessor的Bean后处理器,通过postProcessBeforeInitialization和postProcessAfterInitialization 方法对初始化的Bean进行自定义处理;
那么这十种方式所定义方法执的行顺序是什么?
鄙人经过大量的代码实验得到了一些结果记录下来,以供用到时查阅。
初始化过程中各方法的执行顺序如下:
- 调用构造器 Bean.constructor,进行实例化;
- 调用 Setter 方法,设置属性值;
- 调用 BeanNameAware.setBeanName,设置Bean的ID或者Name;
- 调用 BeanFactoryAware.setBeanFactory,设置BeanFactory;
- 调用 ApplicationContextAware.setApplicationContext,置ApplicationContext;
- 调用BeanPostProcessor的预先初始化方法,如下:
BeanPostProcessor1.postProcessBeforeInitialization
BeanPostProcessor2.postProcessBeforeInitialization
BeanPostProcessor3.postProcessBeforeInitialization
…… - 调用由 @PostConstruct 注解的方法;
- 调用 InitializingBean.afterPropertiesSet;
- 调用 Bean.init-mehod 初始化方法;
- 调用BeanPostProcessor的后初始化方法,如下:
BeanPostProcessor1.postProcessAfterInitialization
BeanPostProcessor2.postProcessAfterInitialization
BeanPostProcessor3.postProcessAfterInitialization
……
容器关闭时,Bean销毁过程中各方法的执行顺序如下:
- 调用由 @PreDestroy 注解的方法
- 调用DisposableBean的destroy();
- 调用定制的destroy-method方法;
实验代码如下:
文件 bean.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
"
default-init-method="init_method" default-destroy-method="destroy_method">
<context:component-scan base-package="spring" />
<bean class="spring.InitHelloWorld2"/>
<bean class="spring.InitHelloWorld"/>
<bean id="hello" class="spring.Hello1" >
<property name="message" value="ccccccccc"/>
</bean>
</beans>
文件 Hello1.java 的代码如下:
package spring;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
public class Hello1 implements InitializingBean, BeanNameAware,BeanFactoryAware,ApplicationContextAware,DisposableBean{
private String message;
public String getMessage(){
return message;
}
public void setMessage(String message){
System.out.println("Hello1 setMessage");
this.message = message;
}
public Hello1(){
System.out.println("Hello1 构造器");
}
public void hello(){
System.out.println("Hello1 我是:" + message);
}
public void init_method(){
System.out.println("Hello1 init-method ");
}
@PostConstruct
public void postConstruct(){
System.out.println("Hello1 @PostConstruct");
}
@Override
public void afterPropertiesSet() throws Exception{
System.out.println("Hello1 InitializingBean.afterPropertiesSet");
}
@Override
public void setBeanName(String s){
System.out.println("Hello1 setBeanName " + s);
}
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException{
System.out.println("Hello1 setBeanFactory " );
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException{
System.out.println("Hello1 setApplicationContext " );
}
@Override
public void destroy() throws Exception{
System.out.println("Hello1 DisposableBean.destroy " );
}
public void destroy_method() throws Exception{
System.out.println("Hello1 destroy-method " );
}
@PreDestroy
public void preDestroy() throws Exception{
System.out.println("Hello1 @PreDestroy " );
}
}
文件 InitHelloWord.java 的代码如下:
package spring;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.core.Ordered;
public class InitHelloWorld implements BeanPostProcessor,Ordered{
@Override
public Object postProcessBeforeInitialization(Object o, String s) throws BeansException{
System.out.println("BeanPostProcessor1.postProcessBeforeInitialization:" + s);
return o;
}
@Override
public Object postProcessAfterInitialization(Object o, String s) throws BeansException{
System.out.println("BeanPostProcessor1.postProcessAfterInitialization:" + s);
return o;
}
@Override
public int getOrder(){
return 10;
}
}
文件 InitHelloWord2.java 的代码如下:
package spring;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.core.Ordered;
public class InitHelloWorld2 implements BeanPostProcessor,Ordered{
@Override
public Object postProcessBeforeInitialization(Object o, String s) throws BeansException{
System.out.println("BeanPostProcessor2.postProcessBeforeInitialization:" + s);
return o;
}
@Override
public Object postProcessAfterInitialization(Object o, String s) throws BeansException{
System.out.println("BeanPostProcessor2.postProcessAfterInitialization:" + s);
return o;
}
@Override
public int getOrder(){
return 20;
}
}
文件 MainApp.java 代码如下:
package spring;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp{
public static void main(String... args){
AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
Hello1 hello = (Hello1) ctx.getBean("hello");
hello.hello();
ctx.close();
}
}
代码执行结果如下:
Hello1 构造器
Hello1 setMessage
Hello1 setBeanName hello
Hello1 setBeanFactory
Hello1 setApplicationContext
BeanPostProcessor1.postProcessBeforeInitialization:hello
BeanPostProcessor2.postProcessBeforeInitialization:hello
Hello1 @PostConstruct
Hello1 InitializingBean.afterPropertiesSet
Hello1 init-method
BeanPostProcessor1.postProcessAfterInitialization:hello
BeanPostProcessor2.postProcessAfterInitialization:hello
Hello1 我是:ccccccccc
Hello1 @PreDestroy
Hello1 DisposableBean.destroy
Hello1 destroy-method
链接:https://www.jianshu.com/p/80d4fa132747
在Spring Bean的生命周期中各方法的执行顺序的更多相关文章
- Spring Bean的生命周期,《Spring 实战》书中的官方说法
连着两天的面试 ,都问到了 Spring 的Bean的生命周期,其中还包括 昨晚一波阿里的电话面试.这里找到了Spring 实战中的官方说法.希望各位要面试的小伙伴记住,以后有可能,或者是有时间 去看 ...
- 你知道Spring是怎么将AOP应用到Bean的生命周期中的吗?
聊一聊Spring是怎么将AOP应用到Bean的生命周期中的? 本系列文章: 听说你还没学Spring就被源码编译劝退了?30+张图带你玩转Spring编译 读源码,我们可以从第一行读起 你知道Spr ...
- Spring Bean的生命周期(非常详细)
Spring作为当前Java最流行.最强大的轻量级框架,受到了程序员的热烈欢迎.准确的了解Spring Bean的生命周期是非常必要的.我们通常使用ApplicationContext作为Spring ...
- Spring Bean的生命周期相关博客
最近得面试题一直 问 Spring 得生命周期,鉴于自己还未阅读过源码 所以只能是自己 背一波了.属实不懂硬背得作用,但是无奈被各位面试官打败了.等以后有时间了 一定要阅读几遍spring的 源码 有 ...
- Spring学习手札(四)谈谈Spring Bean的生命周期及作用域
在Spring中,那些组成应用程序的主体以及由Spring IoC容器所管理的对象,被称之为Bean.Bean与应用程序中其他对象(比如自己创建类)的区别就是,Bean是由IoC容器创建于销毁的.在S ...
- Spring Bean的生命周期详解(转)
Spring作为当前Java最流行.最强大的轻量级框架,受到了程序员的热烈欢迎.准确的了解Spring Bean的生命周期是非常必要的.我们通常使用ApplicationContext作为Spring ...
- Spring动态代理及Spring Bean的生命周期
数组添加值 public class DiTest { /** * 数组 */ private String [] arrays; /** * List:集合 */ private List<I ...
- Spring(三)--Spring bean的生命周期
Spring bean的生命周期 ApplicationContext Bean生命周期流程 1.需要的实体类 ackage com.xdf.bean; import org.springframew ...
- 第37讲 谈谈Spring Bean的生命周期和作用域
在企业应用软件开发中,Java 是毫无争议的主流语言,开放的 Java EE 规范和强大的开源框架功不可没,其中 Spring 毫无疑问已经成为企业软件开发的事实标准之一.今天这一讲,我将补充 Spr ...
随机推荐
- 判断CString 字符串里面是否全部为数字
//原理就是去除0-9的数字,判断去除数字后的字符串是否为空,如果为空,说明字符串全部都是为数字,否则得话,就不是. strOutTimeOnNum = strouttime.TrimLeft( _T ...
- Eclipse中Debug时鼠标悬停不能查看变量值解决办法
问题描述:Eclipse在Debug模式下,当鼠标移动到某个变量上面时不自动显示该变量对应的值. 解决方法:在Eclipse中点击 Window->Preferences->Java-&g ...
- 一行一行源码分析清楚AbstractQueuedSynchronizer
“365篇原创计划”第二十四篇. 今天呢!灯塔君跟大家讲: 一行一行源码分析清楚AbstractQueuedSynchronizer 在分析 Java 并发包 java.util.concurren ...
- 关于前端数据&逻辑的思考
最近重构了一个项目,一个基于redux模型的react-native项目,目标是在混乱的代码中梳理出一个清晰的结构来,为了实现这个目标,首先需要对项目的结构做分层处理,将各个逻辑分离出来,这里我是基于 ...
- 常见的H5移动端Web页面Bug问题解决方案总汇
解决jquery ajax调用远程接口的跨域问题 首先,接口必须允许远程调用.这是后端或者运维的事情.你必须保证你得到的一个接口是允许远程调用的.否则,就没啥了. $.ajax({ type:'get ...
- Layui数据表格加入自定义扩展方法(重新渲染Render当前页数据)
具体开发中遇到的问题如下, 数据表格的重新渲染或重新加载会导致当前操作的分页 或 配置被清空.我正在操作第5页,重新渲染后就回到了最原始第1页. 需要达到的效果是: 不调用接口,仅仅只是从table. ...
- Aaronson,又是思维题
题目: Recently, Peter saw the equation x0+2x1+4x2+...+2mxm=nx0+2x1+4x2+...+2mxm=n. He wants to find a ...
- 文件读取一些payload
Windows: C:boot.ini //查看系统版本 C:WindowsSystem32inetsrvMetaBase.xml //IIS配置文件 C:Windowsrepairsam //存储系 ...
- Vue---day05
目录 2. 客户端项目搭建 2.1 创建项目目录 2.2 初始化项目 2.3 安装路由vue-router 2.3.1 下载安装路由组件 2.3.2 配置路由 2.3.2.1 初始化路由对象 2.3. ...
- ASP.NET MVC Route详解
在MVC3.0版本的时候,微软终于引入了第二种模板引擎:Razor.在这之前,我们一直在使用WebForm时代沿留下来的ASPX引擎或者第三方的NVelocity模板引擎.Razor在减少代码冗余.增 ...