SpringBoot的Bean的生命周期

什么是Bean的生命周期,就是Bean从创建到销毁的过程。
springboot中Bean的生命周期的过程:
1.Bean的定义
2.Bean的初始化
3.Bean的生存期
4.Bean的销毁

Bean的生命周期过程描述:
首先Spring在实例化Bean的时候,会先调用它的构造函数,进行Bean的实例化,然后进行Bean的初始化,Bean的初始化过程经过三个阶段,初始化之前,初始化和初始化之后,这就是初始化过程,然后开始利用Bean经行业务逻辑处理,最后容器正常关闭,spring开始销毁Bean,Bean的销毁过程相对比较简单,调用DisposableBeanAdapter.destroy()方法,该方法有三个地方比较重要,分别出发Bean的生命周期方法,分别是:
1.processor.postProcessBeforeDestruction(this.bean,this.beanName);
2.((DisposableBean)bean).destroy();
3.invokeCustomDestroyMethod(this.destroyMethod);

实例演示:

User类,一个普通的Bean,用来添加到容器中,观察其生命周期

package com.itbofeng.bean;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
public class User implements InitializingBean , DisposableBean {
    private String name;
    public User() {
        System.out.println("调用Bean的函数(constructor)");
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        System.out.println("调用Bean的函数(setName/setAttribute)");
        this.name = name;
    }
    @PostConstruct
    public void postConstruct(){
        System.out.println("调用Bean的函数(postConstruct)");
    }
    //MainConfig中@Bean 的initMethod
    public void initMethod(){
        System.out.println("调用Bean的函数(initMethod)");
    }
    //InitializingBean接口的方法afterPropertiesSet
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("调用Bean的函数(afterPropertiesSet)");
    }
    @PreDestroy
    public void preDestroy(){
        System.out.println("调用Bean的函数(preDestroy)");
    }
    //DisposableBean接口的方法destroy
    @Override
    public void destroy() throws Exception {
        System.out.println("调用Bean的函数(destroy)");
    }
    //MainConfig中@Bean 的destroyMethod
    public void destroyMethod(){
        System.out.println("调用Bean的函数(destroyMethod)");
    }
}



CustomBeanPostProcessor类,用来观察BeanPostProcessor的运行时期

package com.itbofeng.bean;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
@Component
public class CustomBeanPostProcessor implements BeanPostProcessor {
    @Nullable
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if(bean.getClass()==User.class){
            System.out.println("调用postProcessBeforeInitialization...");
        }
        return bean;
    }
    @Nullable
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if(bean.getClass()==User.class){
            System.out.println("调用postProcessAfterInitialization...");
        }
        return bean;
    }
}



MainConfig类,springboot程序运行的主类,并将User和CunstomBeanPostProcessor加入到容器中

package com.itbofeng;
import com.itbofeng.bean.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class MainConfig {
    public static void main(String[] args) {
        SpringApplication.run(MainConfig.class,args);
    }
    @Bean(initMethod = "initMethod",destroyMethod = "destroyMethod")
    public User user(){
        return new User();
    }
}
}



运行的结果为:
image.png




暂无评论