解释Spring框架中bean的生命周期
一、Bean生命周期的流程图

二、spring的生命周期
spring生命周期中的阶段,包括初始化、使用、销毁。
1、初始化阶段
1)调用bean的构造函数,创建实例;
2)进行参数依赖注入;
3)若实现org.springframework.beans.BeanNameAware接口,则调用BeanNameAware的setBeanName()方法;
4)若实现org.springframework.beans.factory.BeanClassLoaderAware接口,则调用BeanClassLoaderAware的setBeanClassLoader()方法;
5)若实现org.springframework.context.ApplicationContextAware接口,则调用ApplicationContextAware的setApplicationContext()方法;
6)若使用了注解@PostConstruct,则调相应方法;
7)若实现org.springframework.beans.factory.InitializingBean接口,则调用InitializingBean接口的afterPropertiesSet方法;
8)若bean定义的使用了initMethod,则调相应方法;
9)若实现org.springframework.beans.factory.config.BeanPostProcessor接口,则调用BeanPostProcessor的postProcessBeforeInitialization()方法和 postProcessAfterInitialization方法;
2、使用阶段
1)bean在项目的使用;
3、销毁阶段
1)若使用注解@PreDestroy,则调用相应方法;
2)若bean定义中配置了destroyMethod,则调用相应方法;
3)若实现org.springframework.beans.factory.DisposableBean接口,则调用DisposableBean接口的destroy方法;
四、bean生命周期示例
1、定义bean文件 SpringBeanLife.class
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
public class SpringBeanLife implements BeanNameAware, BeanClassLoaderAware, ApplicationContextAware,
InitializingBean, BeanPostProcessor, DisposableBean {
private String id;
/******************* 下面是方法按时间执行的先后顺序 *************************/
//初始化
SpringBeanLife() {
System.out.println("bean init");
}
//参数注入
public void setId(String id) {
this.id = id;
System.out.println("invoke set method");
}
//来自BeanNameAware
@Override
public void setBeanName(String name) {
System.out.println("invoke setBeanName");
}
//来自BeanClassLoaderAware
@Override
public void setBeanClassLoader(ClassLoader classLoader) {
System.out.println("invoke setBeanClassLoader");
}
//来自ApplicationContextAware
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
System.out.println("invoke setApplicationContext");
}
//来自注解@PostConstruct
@PostConstruct
public void postConstructMethod() {
System.out.println("invoke postConstructMethod");
}
//来自InitializingBean
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("invoke afterPropertiesSet");
}
//来自配置initMethod = "initMethod"
public void initMethod() {
System.out.println("invoke init-method");
}
//来自BeanPostProcessor
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("invoke postProcessBeforeInitialization");
return bean;
}
//来自BeanPostProcessor
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("invoke postProcessAfterInitialization");
return bean;
}
//bean使用(如此时调用了下面的use方法)
public void use() {
System.out.println("use bean");
}
//来自注解@PreDestroy
@PreDestroy
public void preDestroyMethod() {
System.out.println("invoke preDestroyMethod");
}
//来自bean定义中的配置destroyMethod = "destoryMethod"
public void destoryMethod() {
System.out.println("invoke destory-method");
}
//来自DisposableBean
@Override
public void destroy() throws Exception {
System.out.println("invoke destroy");
}
}
2、测试
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
@Configurable
public class SpringConfig {
@Bean(initMethod = "initMethod", destroyMethod = "destoryMethod")
public SpringBeanLife springBeanLife() {
SpringBeanLife bean = new SpringBeanLife();
bean.setId("001");
return bean;
}
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
SpringBeanLife bean = ctx.getBean(SpringBeanLife.class);
bean.use();
ctx.close();
}
}
输出:
bean init
invoke set method
invoke setBeanName
invoke setBeanClassLoader
invoke setApplicationContext
invoke postConstructMethod
invoke afterPropertiesSet
invoke init-method
invoke postProcessBeforeInitialization
invoke postProcessAfterInitialization
invoke postProcessBeforeInitialization
invoke postProcessAfterInitialization
use bean
invoke preDestroyMethod
invoke destroy
invoke destory-method
解释Spring框架中bean的生命周期的更多相关文章
- 解释Spring框架中bean的生命周期?
Spring容器 从XML 文件中读取bean的定义,并实例化bean. Spring根据bean的定义填充所有的属性. 如果bean实现了BeanNameAware 接口,Spring 传递bean ...
- 解释 Spring 框架中 bean 的生命周期?
Spring 容器 从 XML 文件中读取 bean 的定义,并实例化 bean. Spring 根据 bean 的定义填充所有的属性. 如果 bean 实现了 BeanNameAware 接口,Sp ...
- 解释 Spring 框架中 bean 的生命周期?
Spring 容器 从 XML 文件中读取 bean 的定义,并实例化 bean. Spring 根据 bean 的定义填充所有的属性. 如果 bean 实现了 BeanNameAware 接口,Sp ...
- spring框架中Bean的生命周期
一.Bean 的完整生命周期 在传统的Java应用中,bean的生命周期很简单,使用Java关键字 new 进行Bean 的实例化,然后该Bean 就能够使用了.一旦bean不再被使用,则由Java自 ...
- Spring 容器中 Bean 的生命周期
Spring 容器中 Bean 的生命周期 1. init-method 和 destory-method 方法 Spring 初始化 bean 或销毁 bean 时,有时需要作一些处理工作,因此 s ...
- (spring-第1回【IoC基础篇】)Spring容器中Bean的生命周期
日出日落,春去秋来,花随流水,北雁南飞,世间万物皆有生死轮回.从调用XML中的Bean配置信息,到应用到具体实例中,再到销毁,Bean也有属于它的生命周期. 人类大脑对图像的认知能力永远高于文字,因此 ...
- IoC基础篇(一)--- Spring容器中Bean的生命周期
日出日落,春去秋来,花随流水,北雁南飞,世间万物皆有生死轮回.从调用XML中的Bean配置信息,到应用到具体实例中,再到销毁,Bean也有属于它的生命周期. 人类大脑对图像的认知能力永远高于文字,因此 ...
- Spring容器中bean的生命周期以及关注spring bean对象的后置处理器:BeanPostProcessor(一个接口)
Spring IOC 容器对 Bean 的生命周期进行管理的过程: 1.通过构造器或工厂方法创建 Bean 实例 2.为 Bean 的属性设置值和对其他 Bean 的引用 3.将 Bean 实例传递给 ...
- spring ApplicationContext中Bean的生命周期
AbstractApplicationContext Spring的AbstractApplicationContext是ApplicationContext的抽象实现类,该抽象类的refresh方法 ...
- Spring容器中Bean的生命周期
随机推荐
- 多方安全计算(4):MPC万能积木-秘密共享
学习&转载文章:多方安全计算(4):MPC万能积木-秘密共享 前言 在之前的文章(多方安全计算(3)MPC万能钥匙:混淆电路中,我们对MPC中一类通用方案混淆电路(GC)与密文比较策略做了介绍 ...
- e-prime2.0 安装
先卸载之前的 卸载 1.点开之前安装的文件.exe 2.选择remove,即卸载 打开注册表 1.开始-运行:输入regedit 2.然后找到并删除注册表中: HKEY_CURRENT_USER\So ...
- Kotlin:【Map集合】集合创建、集合遍历、元素增加
to本身是一个函数
- Flu PG walkthrough Intermediate
nmap ┌──(root㉿kali)-[/home/ftpuserr] └─# nmap -p- -A 192.168.192.41 Starting Nmap 7.94SVN ( https:// ...
- arthas进行java应用不停服务情况下的class文件热部署更新
我们在工作的过程中会遇到java应用已经部署或升级投入使用,发现某个单元文件有bug需要修正,但是客户的应用目前不能停止,而且不能因为一个非致命的bug来进行整个平台的一次升级.我们需要进行单文件的更 ...
- Rust多线程中安全的使用变量
在Rust语言中,一个既引人入胜又可能带来挑战的特性是闭包如何从其所在环境中捕获变量,尤其是在涉及多线程编程的情境下. 如果尝试在不使用move关键字的情况下创建新线程并传递数据至闭包内,编译器将很可 ...
- H5调用手机拨打电话的功能
里面加上: 我没有写也是可以的 <meta name="format-detection" content="telephone=yes"/> 该标 ...
- DBeaver连接SqlServer报“The server selected protocol version TLS10 is not accepted by client prefere”的错误
1.问题描述 DBeaver在连接SqlServer时,出现如下图所示的错误: The server selected protocol version TLS10 is not accepted b ...
- PHP变量与变量作用域
PHP变量与变量作用域 1. 变量的基本概念 在PHP中,变量用于存储各种类型的数据,如字符串.整数.浮点数.布尔值.数组和对象等.变量名以美元符号$开头,后面跟着一个或多个字符(变量名).例如: & ...
- FreeSql学习笔记——12.执行Sql
前言 FreeSql支持通过Sql语句配合ISelect生成最终的Sql语句,也可以执行原生自定义Sql语句,使操作更灵活:通过sql语句与Iselect配合使用更好控制sql语句: 简单查询 _ ...