在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 ...
随机推荐
- 你想了解的 HTTPS 都在这里
HTTP 协议仅仅制定了互联网传输的标准,简化了直接使用 TCP 协议进行通信的难度.有关 HTTP 协议相关的讲解请看前面两节: HTTP 协议详解 HTTP协议详解(二) less is more ...
- Typography convention
1 h1 Chapter title centered,number three in bold,used ##. 1.1 h2 The chapter is a section, and the s ...
- Spring拦截器和SpringAop实现
一.拦截器 1.aop是面向切面编程,原理是java的发射技术. 2.分为三类,before.after.arround 3.springMvc为我们提供了一个适配器HandlerIntercepto ...
- 构建者模式Builder创建对象
构建者(Builder)设计模式(又叫生成器设计模式): 当一个类的内部数据过于复杂的时候(通常是负责持有数据的类,比如Config.VO.PO.Entity...),要创建的话可能就需要了解这个类的 ...
- 并发工具CyclicBarrier源码分析及应用
本文首发于微信公众号[猿灯塔],转载引用请说明出处 今天呢!灯塔君跟大家讲: 并发工具CyclicBarrier源码分析及应用 一.CyclicBarrier简介 1.简介 CyclicBarri ...
- JS数据类型判断的几种方法
JS数据类型判断 JavaScript 中常见数据类型有Number.String.Boolean.Object.Array.Json.Function.Date.RegExp.Error.undef ...
- Netty 中的内存分配浅析-数据容器
本篇接续前一篇继续讲 Netty 中的内存分配.上一篇 先简单做一下回顾: Netty 为了更高效的管理内存,自己实现了一套内存管理的逻辑,借鉴 jemalloc 的思想实现了一套池化内存管理的思路: ...
- ansible 2.7 API
# coding:utf-8 # @Time : 2019-01-14 15:22 # @Author : 小贰 # @FileName: ansible_sync_hosts.py # @funct ...
- 解决安装mysql 提示msvcr100.dill 丢失,的最快方法
我也是在学习mysql的时候遇到的这个问题,很多人也遇到了,于是在百度找解决方案 看到有人论坛中写道,用 360安全卫士,可以修复于是我下载了360安全卫士尝试修复, 在人工解答中搜索dll修复,也修 ...
- 题解:2018级算法第二次上机 Zexal的排座位
题目描述: 样例: 实现解释: 一道看似复杂但实际既是斐波那契变形的题目 知识点:递推,斐波那契 通过问题的描述,可以得到以下规律:(除了座位数为一时)男生坐最后时,倒数第二个一定是女生:女生坐最后, ...