2.spring bean生命周期

目录

image.png

1.bean的生命周期

创建->初始化->销毁

graph TB
a("容器启动")-->b("bean实例化")
b-->c("设置对象属性")
c-->d("检查Aware相关接口并设置相关依赖")
d-->e("BeanPostProcesser前置处理")
e-->f("InitializingBean调用#afterPropertiesSet")
f-->m("调用initMethod指定方法")
m-->g("InitializingBean调用#postProcessAfterInitialization")
g-->h("放置map中")
h-->i("DisposableBean#destroy")
i-->j("调用destroyMethod指定方法")
j-->en("结束")

1.@Bean initMethod和destroyMethod

指定该bean的初始化和销毁方法

2.实现接口InitializingBean,DisposableBean

3.JSR250

使用注解 @PostConstruct初始化操作 @PreDestroy销毁操作

4.BeanPostProcessor

1.代码

1.1 bean对象

public class MyInitBean implements InitializingBean, DisposableBean , BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("-------Before-BeanPostProcessor---"+bean+"     "+beanName);
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("-------After-BeanPostProcessor---"+bean+"     "+beanName);

        return bean;
    }
    private void init() {
        System.out.println(" -----指定方法 InitEntity3 initMethod start*****************");
    }

    private void end() {
        System.out.println("-----指定方法 InitEntity3 initMethod destroyMethod*****************");
    }

    public MyInitBean() {
        System.out.println("-----构造器");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("-----接口DisposableBean destroy");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("-----接口InitializingBean afterPropertiesSet");
    }

    @PostConstruct
    public void PostConstruct() {
        System.out.println("------ 注解@PostConstruct--");
    }

    @PreDestroy
    public void PreDestroy() {
        System.out.println("------ 注解@PreDestroy--");
    }


}

1.2 配置类

@Configuration
public class MyConfiguation {
    @Bean(initMethod = "init",destroyMethod = "end")
    public MyInitBean myInitBean() {
        return new MyInitBean();
    }
}

1.3 测试类

    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyConfiguation.class);
        System.out.println("_--------------------------------_");
        applicationContext.close();
        System.out.println("_--------------------------------_");
    }

1.4 结果

-----构造器
------ 注解@PostConstruct--
-----接口InitializingBean afterPropertiesSet
 -----指定方法 InitEntity3 initMethod start*****************
-------Before-BeanPostProcessor---org.springframework.context.event.EventListenerMethodProcessor@276438c9     org.springframework.context.event.internalEventListenerProcessor
-------After-BeanPostProcessor---org.springframework.context.event.EventListenerMethodProcessor@276438c9     org.springframework.context.event.internalEventListenerProcessor
-------Before-BeanPostProcessor---org.springframework.context.event.DefaultEventListenerFactory@33b37288     org.springframework.context.event.internalEventListenerFactory
-------After-BeanPostProcessor---org.springframework.context.event.DefaultEventListenerFactory@33b37288     org.springframework.context.event.internalEventListenerFactory
_--------------------------------_
------ 注解@PreDestroy--
-----接口DisposableBean destroy
-----指定方法 InitEntity3 initMethod destroyMethod*****************
_--------------------------------_

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 此文主要讲了Spring中,Bean的生命周期,初始化动作,销毁动作是如何执行。 以及关系生命周期各个接口的作用和...
    _Zy阅读 764评论 0 5
  • 前言 Spring 很复杂,很多东西都很难完全深入了解,这里写下Bean 的初始化的分析,主要是为了解Spring...
    JiaJianHuang阅读 1,105评论 0 1
  • 文章作者:Tyan博客:noahsnail.com | CSDN | 简书 3.6 Customizing the...
    SnailTyan阅读 901评论 0 3
  • 本文章主要围绕bean的生命周期来进行讲解,比如bean的实例化之后可以做什么,还有bean销毁之前该做什么 1....
    Mrsunup阅读 712评论 0 0
  • 曾经因为喜欢一个人 喜欢了一座城市 现在也因为放弃一个人 厌恶了那座城市 我给了我能给的所有 到最后什么也没有留下...
    yangzhenyu97阅读 169评论 0 1