解释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的生命周期
随机推荐
- refs转发
ref 转发不但可以转发指向具体的dom组件,也可以指向class组件的实例 import React from 'react' import ReactDOM from 'react-dom'; / ...
- 使用坦克PWA访问助手为自己的局域网应用快速配置免费域名
这篇教程描述如何使用坦克PWA访问助手.这篇文章简称坦克PWA访问助手为PWA助手.PWA结合了DNS服务器技术和HTTP服务器技术实现,因此它需要系统的53端口和80端口.所以,如果你的电脑有程序占 ...
- C#从数据库中加载照片的
从数据库中读取人员照片信息并加载到图片控件的代码 string conn = "Server=192.168.xx.xx;Database=dbName;User ID=sa;passwor ...
- VM1038:1 (in promise) MiniProgramError {"errMsg":"navigateTo:fail webview count limit exceed"} Object解决办法
在跳转的时候,出现这,有的时候回出现,有的时候不会出现: VM1038:1 (in promise) MiniProgramError {"errMsg":"naviga ...
- linux goreplay流量压测工具
项目地址https://github.com/buger/goreplay 下载wget https://github.com/buger/goreplay/releases/download/v0. ...
- flutter-延时执行
//1秒后这个i行 Future.delayed(Duration(milliseconds: 1000), () { //代码省略 });
- day:3软件测试分类
一.按开发阶段划分 (1)单元测试 (2)集成测试 (3)系统测试 (4)验收测试 二.按查看代码分类 (1)黑盒测试 定义:是一种功能测试,测试中把测试的软件当成一个盒子,不关心盒子内部结构是什么, ...
- 洛谷B4038 [GESP202409 三级] 平衡序列 题解
原题传送门 前言 当我以一种十分激动的心情参加了GESP的2024-9的三级考试时. 打开了此题,然后--自以为是的拿着暴力一顿乱写!然后TLE. 直到结束我还是没有想出来! (太菜了!!!) 以一种 ...
- LLM生成代码后,如何一键合并到源代码中(FastApply技术研究)
背景 在大语言模型越来越火的今天,越来越多的应用场景开始使用大语言模型来解决实际问题.而辅助编程可以算是大语言模型应用得最成功的场景之一了.早先的时候,更多使用的还是代码补全的能力,但是现在,各家产品 ...
- ChatBI≠NL2SQL:关于问数,聊聊我踩过的坑和一点感悟
"如果说数据是新时代的石油,智能问数就是能让普通人也能操作的智能钻井平台." 这里是**AI粉嫩特攻队!** ,这段时间真的太忙了,不过放心,关于从零打造AI工具的coze实操下篇 ...