Spring5源码分析之Bean生命周期

生命周期

  创建  ----> 初始化 ---> 销毁

1. 实例化对象

2. setter注入,执行Bean的属性依赖注入

3. BeanNameAware的setBeanName(), 如果实现该接口,则执行其setBeanName 方法

4.BeanFactoryAware的setBeanFactory(), 如果实现该接口,则执行其setBeanFactory方法

5. BeanPostProcessor的processBeforeInitialization(),如果有关联的processor,则在Bean初始化之前都会执行这个实例的processBeforeInitialization() 方法

6. InitializingBean的afterPropertiesSet(), 如果实现了该接口,则执行其afterPropertiesSet()方法。Bean定义文件中定义init-method

7.BeanPostProcessors的processAfterInitialization(),如果有关联的processor,则在Bean初始化之前都会执行这个实例的processAfterInitialization()方法

8.DisposeablebBean的 destory(),在容器关闭时,如果Bean实现了这个接口,则执行他的destory()方法

9. Bean定义文件中定义destroy-method,在容器关闭时,可以在Bean定义文件中使用“destory-method”定义的方法

注:

 1. 单例默认时在容器被嘉爱时候会初始化

 2. 多例在每次获取Bean的对象时候才会去初始化


 bean 初始化 指的就是对象已经创建里面所有的set方法都已经执行完毕了。 指定方法执行

   @Bean(initMethod , destory)   指定初始化和销毁方法  同时在bean中创建这两个方法

   init是在构造方法之前还是之后执行? 无参构造!  对象先创建完成后才进行初始化!所以先执行无参构造函数!

补充到上面的过程:

  Bean的创建(执行构造函数) --> 初始化(自定义init方法) --> 销毁

  调用close() 方法销毁单例对象  

   注意:IOC容器使用Map结合存储对象,clear() 清除对象

   看源码:

进入查看:

继续点击查看:

看第一个:

持续跟进后就是 集合的 clear 方法了

 我们可以通过实现某些类 去进行初始化的操作!

开发使用的方式:


方法一: 通过@Bean指定init-method 和 destory-method

方法二: 通过让Bean实现InitializingBean(定义初始化逻辑), DisposableBean(定义销毁逻辑)

方法三: 使用JSR250(Java规范,不是Spring的): @PostConstructo: 在Bean创建完成并且赋值完成,来执行初始化方法。 @PreDestory: 在容器销毁Bean之前通知我们进行清理工作


方法二:

Bean:

@Component
public class LifeBean implements InitializingBean, DisposableBean {

    //构造函数
    public LifeBean() {
        System.out.println("LifeBean Constructor");
    }

    /** 接口InitializingBean的方法
     *  //解释 对象有创建 肯定也有给属相赋值的过程!,对象赋值完毕以后才执行该方法  即: (afterPropertiesSet)中文:set方法都走完了时候执行该方法
     * @throws Exception
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        //等同于 @Bean(init =" ")
        //解释 对象有创建 肯定也有给属相赋值的过程!,对象赋值完毕以后才执行该方法
        System.out.println("LifeBean ********> 【InitializingBean.afterPropertiesSet】 ");
    }

    /**
     * 接口DisposableBean 的方法
     * @throws Exception
     */
    @Override
    public void destroy() throws Exception {
        System.out.println("LifeBean ********>【DisposableBean.destroy】");
    }
}

配置和扫包

@Configuration
@ComponentScan("com.toov5.config.beanTest.entity")
public class MyConfig {

}

启动测试:

public class test {
    public test(){

    }
    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext1 = new AnnotationConfigApplicationContext("com.toov5.config");
        applicationContext1.close();
//        System.out.println(applicationContext1);
    }
}

方法三

  注解代替了接口:

Bean:

@Component
public class LifeBean {

    //构造函数
    public LifeBean() {
        System.out.println("LifeBean Constructor");
    }
    @PostConstruct
    public void afterPropertiesSet() throws Exception {
        //等同于 @Bean(init =" ")
        //解释 对象有创建 肯定也有给属相赋值的过程!,对象赋值完毕以后才执行该方法
        System.out.println("LifeBean ********> 【InitializingBean.afterPropertiesSet】 ");
    }

   @PreDestroy
    public void destroy() throws Exception {
        System.out.println("LifeBean ********>【DisposableBean.destroy】");
    }
}

效果是一样的:

  

 Spring Bean生命周期中的后置处理器:  BeanPostProcessor接口


过滤器中,不可以使用注解方式获取Bean对象。 做法: 单独获取上下文ApplicationContext

后置处理器,实现对Bean初始化增强功能

@Component
public class MyApplicationContext implements ApplicationContextAware {
    //开发时候经常做成全局的 进行使用
    private ApplicationContext applicationContext;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        LifeBean lifeBean = applicationContext.getBean("lifeBean", LifeBean.class);
        System.out.println("result————————————————————"+lifeBean.toString());
    }
}

运行后:结果是没问题的

猜你喜欢

转载自www.cnblogs.com/toov5/p/11257106.html