下文要讲的均是spring的默认作用域singleton的bean的生命周期,对spring作用域不了解的可以
 https://blog.csdn.net/hlzdbk/article/details/128811271?spm=1001.2014.3001.5502
什么是SpringBean的生命周期
springBean的生命周期,指的是spring里一个bean对象从出生到死亡的整个过程。解 Spring 生命周期的之后,可以利用 Bean 在其存活期间的指定时刻完成一些相关操作。这种时刻可能有很多,但一般情况下,会在 Bean 被初始化后和被销毁前执行一些相关操作。
SpringBean与普通java对象的生命周期的不同
- 普通java对象
 new的时候创建对象
 对象不再使用,java自动进行垃圾回收
- springBean对象
 实例化
 属性赋值
 初始化
 使用
 销毁
springBean对象生命周期分析
宏观:
 
 微观:
 
 在上面两个图片中,我将springBean的生命周期分别从宏观和微观进行了表示,下面用一个例子来给大家看一下各个不同的生命周期
SpringBean生命周期具体实例
1. 项目结构
此项目结构与springBean作用域项目结构一致,在这里就不多进行赘述
 
2. 具体代码
- PersonBean类,此类为我们要展示的bean对象
package com.tfjy.test2;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
/**
 * @BelongsProject: demo
 * @BelongsPackage: com.tfjy.test2
 * @Author: haolizhuo
 * @Description: Bean的生命周期
 * @CreateTime: 2023-01-29 20:53
 * @Version: 1.0
 */
public class PersonBean implements InitializingBean, BeanFactoryAware, BeanNameAware, DisposableBean {
    /**
     * 身份证号
     */
    private Integer no;
    /**
     * 姓名
     */
    private String name;
    //最先走的方法就是调用构造函数,创建具体对象
    public PersonBean() {
        System.out.println("1.调用构造方法:我出生了!");
    }
    public Integer getNo() {
        return no;
    }
    public void setNo(Integer no) {
        this.no = no;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
        System.out.println("2.设置属性:我的名字叫" + name);
    }
    //BeanNameAware接口,通过这个接口设置bean的id
    @Override
    public void setBeanName(String s) {
        System.out.println("3.调用BeanNameAware#setBeanName方法:我的bean的名字叫做" + s);
    }
    //    BeanFactoryAware 用于注入BeanFactory对象,能访问创建对象的BeanFactory。
    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("4.调用BeanFactoryAware#setBeanFactory方法:注入BeanFactory对象");
    }
    //    InitializingBean 接口
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("6.InitializingBean#afterPropertiesSet方法:开始初始化");
    }
    //在创建bean的时候,会走这个对应的初始化方法
    public void init() {
        System.out.println("7.自定义init方法:自定义初始化");
    }
    //销毁一个bean对象
    @Override
    public void destroy() throws Exception {
        System.out.println("9.DisposableBean#destroy方法:开始销毁");
    }
    public void destroyMethod() {
        System.out.println("10.自定义destroy方法:自定义销毁");
    }
    public void work() {
        System.out.println("Bean使用中~~~~");
    }
}
定制处理器类
package com.tfjy.test2;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
/**
 * @BelongsProject: demo
 * @BelongsPackage: com.tfjy.test2
 * @Author: haolizhuo
 * @Description: 自定义的后处理器
 * @CreateTime: 2023-01-29 20:54
 * @Version: 1.0
 */
public class MyBeanPostProcessor implements BeanPostProcessor {
    //前置处理器,bean对象初始化前的动作
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("5.BeanPostProcessor.postProcessBeforeInitialization方法:bean对象初始化之前的动作");
        return bean;
    }
    //后置处理器,创建bean对象结束后走这里
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("8.BeanPostProcessor#postProcessAfterInitialization方法:bean对象初始化之后的动作");
        return bean;
    }
}
springBean配置文件,在配置文件中通过。 init-method="init" destroy-method="destroyMethod"分别为personBean对象配置初始化和销毁两个生命周期。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean name="myBeanPostProcessor" class="com.tfjy.test2.MyBeanPostProcessor" />
    <bean name="personBean" class="com.tfjy.test2.PersonBean"
          init-method="init" destroy-method="destroyMethod">
        <property name="no" value= "80669865"/>
        <property name="name" value="张铁钢" />
    </bean>
</beans>
通过springBean对象生命周期的学习,我们可以更好的了解spring里,Bean从出生到销毁的过程,并且可以利用一些操作,进行特殊处理。



















