spring 容器中的bean的完整生命周期一共分为十一步完成。

1.bean对象的实例化

2.封装属性,也就是设置properties中的属性值

3.如果bean实现了BeanNameAware,则执行setBeanName方法,也就是bean中的id值

4.如果实现BeanFactoryAware或者ApplicationContextAware ,需要设置setBeanFactory或者上下文对象setApplicationContext

5.如果存在类实现BeanPostProcessor后处理bean,执行postProcessBeforeInitialization,可以在初始化之前执行一些方法

6.如果bean实现了InitializingBean,则执行afterPropertiesSet,执行属性设置之后的操作

7.调用<bean init-method="">执行指定的初始化方法

8.如果存在类实现BeanPostProcessor则执行postProcessAfterInitialization,执行初始化之后的操作

9.执行自身的业务方法

10.如果bean实现了DisposableBean,则执行spring的的销毁方法

11.调用<bean destory-method="">执行自定义的销毁方法。

第五步和第八步可以结合aop,在初始化执行之前或者执行之后执行一些操作。

以上就是springbean的完整生命周期.

代码如下:

public class Man implements BeanNameAware, ApplicationContextAware, InitializingBean, DisposableBean {
private String name; public Man() {
System.out.println("第一步:实例化类");
} public void setName(String name) {
System.out.println("第二步:设置属性");
this.name = name;
} @Override
public void setBeanName(String s) {
System.out.println("第三步:设置bean的名称也就是spring容器中的名称,也就是id值" + name);
} @Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
System.out.println("第四步:了解工厂信息ApplicationContext");
}
//第五步执行初始化之前执行的方法 @Override
public void afterPropertiesSet() throws Exception {
System.out.println("第六步:属性设置后执行的方法");
} public void setup() {
System.out.println("第七步:执行自己配置的初始化方法");
}
//第八步执行初始化之后执行的方法
public void run() {
System.out.println("第九步:执行自身的业务方法");
} @Override
public void destroy() throws Exception {
System.out.println("第十步:执行spring的销毁方法");
} public void destory() {
System.out.println("第十一步:执行自己配置的销毁方法");
} } 指定的类实现了BeanPostProcessor
public class MyBeanPostProcess implements BeanPostProcessor {

    //后处理bean,最重要的两步
@Override
public Object postProcessBeforeInitialization(Object bean, String s) throws BeansException {
System.out.println("第五步:初始化之前执行的方法");
return bean;
} @Override
public Object postProcessAfterInitialization(Object bean, String s) throws BeansException {
System.out.println("第八步:执行初始化之后的方法");
return bean;
}
} applicationContext.xml中的配置
<bean id="man" class="com.itheima.ioc.springBeanlife.Man" init-method="setup" destroy-method="destory">
<property name="name" value="张三"/>
</bean>
<bean class="com.itheima.ioc.springBeanlife.MyBeanPostProcess"/>

测试
@Test
public void beanlifeTest(){
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
Man man=(Man)context.getBean("man");
man.run();
context.close();
}

spring Bean的完整生命周期的更多相关文章

  1. 一张图搞懂Spring bean的完整生命周期

    一张图搞懂Spring bean的生命周期,从Spring容器启动到容器销毁bean的全过程,包括下面一系列的流程,了解这些流程对我们想在其中任何一个环节怎么操作bean的生成及修饰是非常有帮助的. ...

  2. spring bean 容器的生命周期是什么样的?

    spring bean 容器的生命周期流程如下: 1.Spring 容器根据配置中的 bean 定义中实例化 bean. 2.Spring 使用依赖注入填充所有属性,如 bean 中所定义的配置. 3 ...

  3. Spring Bean各阶段生命周期的介绍

    一.xml方式配置bean 二.Aware接口 2.1 BeanNameAware 2.2 BeanFactoryAware 2.3 ApplicationContextAware 2.4 Aware ...

  4. [spring] -- bean作用域跟生命周期篇

    作用域 singleton : 唯一 bean 实例,Spring 中的 bean 默认都是单例的. prototype : 每次请求都会创建一个新的 bean 实例. request : 每一次HT ...

  5. Spring注解开发系列Ⅲ --- 生命周期

    Bean的生命周期 Spring Bean 的生命周期在整个 Spring 中占有很重要的位置,掌握这些可以加深对 Spring 的理解. 首先看下生命周期图: 再谈生命周期之前有一点需要先明确: S ...

  6. Spring 了解Bean的一生(生命周期)

    转载 https://blog.csdn.net/w_linux/article/details/80086950 该篇博客就来了解IoC容器下Bean的一生吧,也可以理解为bean的生命周期. ## ...

  7. day38 09-Spring类的完整生命周期及后处理Bean

    可以配置Bean的这个类的初始化和销毁的方法. 如何销毁这个bean?销毁必须得手动地关闭掉容器才行.而且销毁必须是在scope="singleton"下才有效.因为如果你scop ...

  8. Spring中与bean有关的生命周期

    前言 记得以前的时候,每次提起Spring中的bean相关的生命周期时,内心都无比的恐惧,因为好像有很多,自己又理不清楚,然后看网上的帖子,好像都是那么一套,什么beanFactory啊,aware接 ...

  9. 浅尝Spring注解开发_Bean生命周期及执行过程

    Spring注解开发 浅尝Spring注解开发,基于Spring 4.3.12 包含Bean生命周期.自定义初始化方法.Debug BeanPostProcessor执行过程及在Spring底层中的应 ...

随机推荐

  1. 【Python 补充01】Python运算符

    Python运算符 举个简单的例子 4 +5 = 9 . 例子中,4 和 5 被称为操作数,"+" 称为运算符. 1.算术运算符 + - * / # 加减乘除 % # 取模(返回除 ...

  2. echarts之legend-改变图例的图标为自定义图片

    legend:{ show:true, orient:'horizontal', borderColor:'#df3434', borderWidth:2, data:[ { name:'蒸发量', ...

  3. grep -v、-e、-E

    在Linux的grep命令中如何使用OR,AND,NOT操作符呢? 其实,在grep命令中,有OR和NOT操作符的等价选项,但是并没有grep AND这种操作符.不过呢,可以使用patterns来模拟 ...

  4. [LeetCode] 15. 三数之和

    题目链接:https://leetcode-cn.com/problems/3sum/ 题目描述: 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a ...

  5. 20145203盖泽双《网络对抗技术》拓展:注入:shellcode及return-into-libc攻击

    20145203盖泽双<网络对抗技术>拓展:注入:shellcode及return-into-libc攻击 一.注入:shellcode 1.编写一段用于获取Shellcode的C语言代码 ...

  6. SpringBoot前端模板

    Springboot支持thymeleaf.freemarker.JSP,但是官方不建议使用JSP,因为有些功能会受限制,这里介绍thymeleaf和freemarker. 一.thymeleaf模板 ...

  7. 在Mac OS X下使用Apache、PHP、MySQL、Netbeans、Yii

    本文环境: Mac OS X:10.8.4 Apache:2.2.22 PHP:5.3.15 Netbeans:7.3.1 Yii:1.1.14 Mac OS X是内置了Apache服务器的,不过默认 ...

  8. OCR技术浅析-tesserOCR(3)

    tesserOCR使用 tesserOCR是文字识别软件(惠普公司开源) Optical Character Recognition (OCR)即光学字符辨识是把打印文本转换成一个数字表示的过程.它有 ...

  9. input 各种限制

    test 1.限制只能输入或黏贴11位长度的数字 <input onkeyup="this.value=this.value.replace(/\D/g,'')" onaft ...

  10. jenkins集成python时出现"Non-ASCII character '\xe6' in file"错误解决方法

    我的问题: 使用python3.5,在Linux环境下手动执行python文件时不报错,但是用jenkins自动执行时就报"Non-ASCII character '\xe6' in fil ...