bean-生命周期

 
1)、指定初始化和销毁方法;
通过@Bean指定init-method和destroy-method;
2)、通过让Bean实现InitializingBean(定义初始化逻辑),
DisposableBean(定义销毁逻辑);
3)、可以使用JSR250;
@PostConstruct:在bean创建完成并且属性赋值完成;来执行初始化方法
@PreDestroy:在容器销毁bean之前通知我们进行清理工作
4)、BeanPostProcessor【interface】:bean的后置处理器;
在bean初始化前后进行一些处理工作;
postProcessBeforeInitialization:在初始化之前工作
postProcessAfterInitialization:在初始化之后工作

bean的生命周期:
bean创建---初始化----销毁的过程
容器管理bean的生命周期;
我们可以自定义初始化和销毁方法;容器在bean进行到当前生命周期的时候来调用我们自定义的初始化和销毁方法

执行顺序
1.构造(对象创建)
单实例:在容器启动的时候创建对象
多实例:在每次获取的时候创建对象
2.BeanPostProcessor.postProcessBeforeInitialization
3.初始化:对象创建完成,并赋值好,调用初始化方法。。。
4.BeanPostProcessor.postProcessAfterInitialization
5.销毁:
单实例:容器关闭的时候
多实例:容器不会管理这个bean;容器不会调用销毁方法;


 BeanPostProcessor:
遍历得到容器中所有的BeanPostProcessor;挨个执行beforeInitialization,
一但返回null,跳出for循环,不会执行后面的BeanPostProcessor.postProcessorsBeforeInitialization
 BeanPostProcessor原理
populateBean(beanName, mbd, instanceWrapper);给bean进行属性赋值
initializeBean
{
applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
invokeInitMethods(beanName, wrappedBean, mbd);执行自定义初始化
applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
}

Spring底层对 BeanPostProcessor 的使用;
bean赋值,注入其他组件,@Autowired,生命周期注解功能,@Async,xxx BeanPostProcessor;

 

bean注解中 initMethod初始化调用(所有配置属性值都赋完后)、destroyMethod关闭容器调用

配置类

@Configuration
public class MyConfig1 { @Bean(initMethod = "initEntity5",destroyMethod = "destroyEntity5") public Entity5 entity5(){ return new Entity5(); } }
public class Entity5 {
    public void initEntity5(){
        System.out.println("entity5 bean初始化....");
    }
    public void destroyEntity5(){
        System.out.println("entity5 bean被销毁....");
    }
}

输出

@Test
    public void Test1(){
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyConfig1.class);
        Object entity5 = applicationContext.getBean("entity5");
        System.out.println(entity5);
        // 销毁
        applicationContext.close();
    }

输出结果

 

 

 实现接口 InitializingBean初始化调用(所有配置属性值都赋完后), DisposableBean关闭容器调用

配置类

@Configuration
@Import(Entity6.class)public class MyConfig1 {
}
public class Entity6 implements InitializingBean, DisposableBean {
    @Override
    public void destroy() throws Exception {
        System.out.println("entity6 bean初始化....");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("entity6 bean被销毁....");
    }
}

输出

 @Test
    public void Test1(){
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyConfig1.class);
        Object entity6 = applicationContext.getBean("entity6");
        System.out.println(entity6);
        // 销毁
        applicationContext.close();
    }

输出结果

 注解@PostConstruct初始化调用(所有配置属性值都赋完后)、@PreDestroy关闭容器调用

配置类

@Configuration
@Import(Entity7.class)
public class MyConfig1 {
}
public class Entity7 {
    @PostConstruct
    public void initEntity() throws Exception {
        System.out.println("entity7 bean初始化....");
    }
    @PreDestroy
    public void destroy() throws Exception {
        System.out.println("entity7 bean被销毁....");
    }
}

输出

 @Test
    public void Test1(){
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyConfig1.class);
        Object entity7 = applicationContext.getBean("entity7");
        System.out.println(entity7);
        // 销毁
        applicationContext.close();
    }

输出结果

 

 

 

后置处理器 BeanPostProcessor init初始化前后执行

这个后置处理器适用于所有的初始化

配置类

//后置处理器:初始化前后进行处理工作 将后置处理器加入到容器中
@Component // 注入到容器中 我用的是注解 也可以参照之前的方式
public class MyBeanPostProcessor implements BeanPostProcessor { @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { System.out.println("init初始化前执行。。。。"+beanName+"--------->"+bean); return bean; } @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { System.out.println("init初始化后执行。。。。"+beanName+"--------->"+bean); return bean; } }

输出

  @Test
    public void Test1(){
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyConfig1.class);
        String[] definitionNames = applicationContext.getBeanDefinitionNames();
        // 销毁
        applicationContext.close();
    }

输出结果

 

posted @ 2022-06-04 18:07  Dabo丶  阅读(770)  评论(0编辑  收藏  举报