解释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的生命周期
随机推荐
- Ubuntu开启root账户步骤
在VMware中新建一个Ubuntu,经常使用sudo 太麻烦,还是开启root账户吧. 1.打开 终端: 输入下列命令sudo gedit /usr/share/lightdm/lightdm.co ...
- SqlServer中获取字符串中的数字部分
具体SQL如下所示: --获取字符串中的数字部分 CREATE FUNCTION [dbo].[f_GetNumStr] ( @Str NVARCHAR(MAX) ) RETURNS NVARCHAR ...
- [记录点滴]Ionic编译过程的研究
[记录点滴]Ionic编译过程的研究 0x00 摘要 之前研究Ionic编译过程的笔记,发出来做个记录.当时是因为有些图片没有拷贝到应用中,所以需要调试编译过程. 0x01 入口 编译的入口在plat ...
- shell学习之保存数据库
自动备份 Mysql 数据库脚本 #!/bin/bash #author by wxp #used to backup mysql practise USER=root PASSWD=1234 HOS ...
- 小程序开发实战案例五 | 小程序如何嵌入H5页面
在接入小程序过程中会遇到需要将 H5 页面集成到小程序中情况,今天我们就来聊一聊怎么把 H5 页面塞到小程序中. 本篇文章将会从下面这几个方面来介绍: 小程序承载页面的前期准备 小程序如何承载 H5 ...
- 从0搭建Vue3组件库(一): 开篇
前言 这是从0搭建Vue3组件库系列文章第一篇文章,这个系列我曾经写过多篇文章,但是写完之后回过头来再看里面有很多遗漏以及不足之处,所以决定重新梳理这个系列,并从头开始搭建一个完整的Vue3组件库工程 ...
- C# 钩子函数使用
1. 什么是钩子 hook(钩子)是windows提供的一种消息处理机制平台,是指在程序正常运行中接受信息之前预先启动的函数,用来检查和修改传给该程序的信息,(钩子)实际上是一个处理消息的程序段,通过 ...
- 多项式算法再探:FMT 和 FWT
我们知道,FFT 和 NTT 可以用来解决下面这种问题: \[c_k=\sum_{i+j=k}a_ib_j \] 不过,这并不是卷积的全部形态,比如下面这种: \[c_k=\sum_{i*j=k}a_ ...
- android无障碍开发 企业微信 机器人
实现 Android 无障碍开发 企业微信 机器人 作为一名新入行的开发者,你可能对如何开发一个支持企业微信的无障碍机器人感到迷茫.在这篇文章中,我将为你详细讲解实现这一功能的流程和代码示例. 流程概 ...
- Git 远程仓库地址修改了怎么办?
项目迁移了一波仓库地址,从自建的git-lab到gitee,所以远程仓库地址发生了变更. 命令: git remote -v # 查看本地配置的远程仓库地址,针对下图中的origin,有的人起名字可能 ...