一、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的生命周期的更多相关文章

  1. 解释Spring框架中bean的生命周期?

    Spring容器 从XML 文件中读取bean的定义,并实例化bean. Spring根据bean的定义填充所有的属性. 如果bean实现了BeanNameAware 接口,Spring 传递bean ...

  2. 解释 Spring 框架中 bean 的生命周期?

    Spring 容器 从 XML 文件中读取 bean 的定义,并实例化 bean. Spring 根据 bean 的定义填充所有的属性. 如果 bean 实现了 BeanNameAware 接口,Sp ...

  3. 解释 Spring 框架中 bean 的生命周期?

    Spring 容器 从 XML 文件中读取 bean 的定义,并实例化 bean. Spring 根据 bean 的定义填充所有的属性. 如果 bean 实现了 BeanNameAware 接口,Sp ...

  4. spring框架中Bean的生命周期

    一.Bean 的完整生命周期 在传统的Java应用中,bean的生命周期很简单,使用Java关键字 new 进行Bean 的实例化,然后该Bean 就能够使用了.一旦bean不再被使用,则由Java自 ...

  5. Spring 容器中 Bean 的生命周期

    Spring 容器中 Bean 的生命周期 1. init-method 和 destory-method 方法 Spring 初始化 bean 或销毁 bean 时,有时需要作一些处理工作,因此 s ...

  6. (spring-第1回【IoC基础篇】)Spring容器中Bean的生命周期

    日出日落,春去秋来,花随流水,北雁南飞,世间万物皆有生死轮回.从调用XML中的Bean配置信息,到应用到具体实例中,再到销毁,Bean也有属于它的生命周期. 人类大脑对图像的认知能力永远高于文字,因此 ...

  7. IoC基础篇(一)--- Spring容器中Bean的生命周期

    日出日落,春去秋来,花随流水,北雁南飞,世间万物皆有生死轮回.从调用XML中的Bean配置信息,到应用到具体实例中,再到销毁,Bean也有属于它的生命周期. 人类大脑对图像的认知能力永远高于文字,因此 ...

  8. Spring容器中bean的生命周期以及关注spring bean对象的后置处理器:BeanPostProcessor(一个接口)

    Spring IOC 容器对 Bean 的生命周期进行管理的过程: 1.通过构造器或工厂方法创建 Bean 实例 2.为 Bean 的属性设置值和对其他 Bean 的引用 3.将 Bean 实例传递给 ...

  9. spring ApplicationContext中Bean的生命周期

    AbstractApplicationContext Spring的AbstractApplicationContext是ApplicationContext的抽象实现类,该抽象类的refresh方法 ...

  10. Spring容器中Bean的生命周期

随机推荐

  1. python脚本抓取省市县区乡镇村庄(五级行政区划)

    用python脚本抓取省市县区乡镇村庄(五级行政区划)的过程如下: 1,抓取国家统计局官网上的行政区划名称和对应的代码(5级是不同的网页,所以涉及多层跳转): 2,数据量大约几十万条,频繁访问考虑防屏 ...

  2. 在线客服系统 QPS 突破 240/秒,连接数突破 4000,日请求数接近1000万次,.NET 多线程技术的高性能实践

    背景 我在业余时间开发了一款自己的独立产品:升讯威在线客服与营销系统.陆陆续续开发了几年,从一开始的偶有用户尝试,到如今的 QPS 突破 240 次/秒,连接数突破 4000,日请求数接近 1000 ...

  3. 使用PythonDEAP库实现简单遗传算法

    ​ 本人博客食用体验更佳哦 DEAP(Distributed Evolutionary Algorithms in Python)是一个用于快速原型设计和实验的进化计算框架.它支持多种进化算法,包括遗 ...

  4. 天翼云存储资源盘活系统HBlock,全面释放企业数据价值

    9月6日,天翼云与科技媒体InfoQ联合举办的以"存储难题新解法,揭秘极/致易用的HBlock"为主题的线上技术分享会圆满落幕.天翼云国际业务事业部研发专家武志民与存储产品线总监魏 ...

  5. Jenkins插件:Generic Webhook Trigger

    Jenkins插件:Generic Webhook Trigger 作为一名软件测试工程师,在日常工作中,我经常需要使用Jenkins来进行持续集成和持续部署(CI/CD).而Jenkins的众多插件 ...

  6. oracle 删除过期归档脚本

    一.定时任务 crontab -e 编辑 每周6凌晨3点执行脚本 0 3 * * 6 . /home/oracle/scripts/arch_delete_before_60days_arch.sh ...

  7. mac安装nodejs、npm包设置

    一.安装nodejs 1.下载自己系统的nodejs,我选择18.20版本 https://nodejs.cn/download/ 二.设置 1.设置镜像源: npm config set regis ...

  8. druid 连接池参数说明

    一.参数配置说明 属性 说明 建议值 url 数据库的jdbc连接地址.一般为连接oracle/mysql.示例如下:     mysql : jdbc:mysql://ip:port/dbname? ...

  9. 浏览器自动化与AI Agent结合项目browser-use初探

    browser-use介绍 browser-use是将您的 AI 代理连接到浏览器的最简单方式.它通过提供一个强大且简单的接口来实现 AI 代理访问网站的自动化. GitHub地址:https://g ...

  10. Drasi Reactions SDK

    Drasi Reactions SDK 是一个跨语言的开发工具包,用于实现和处理 Drasi 平台的 Reactions(反应器)功能.该 SDK 目前支持三种主流编程语言:JavaScript/Ty ...