spring bean生命周期-续

2020-12-21  本文已影响0人  何德何能者

继续上一篇文章 spring bean生命周期

除了bean自身实例化/初始化过程有对应的方法监听外,spring还提供了在不通阶段获取不同信息的能力
大概有两类

postProcessor

postProcessor类提供了在bean处理流程用户自定义的能力。主要接口有BeanFactoryProcessor, BeanPostProcessor

Aware

aware接口,提供了bean感知容器状态的能力。主要接口有BeanFactoryAware,EnvironmentAware等

image.png

代码

@Component
public class UserBean implements BeanDefinitionRegistryPostProcessor,
        DestructionAwareBeanPostProcessor,
        InstantiationAwareBeanPostProcessor,
        BeanFactoryAware,
        EnvironmentAware,
        SmartLifecycle,
        InitializingBean,
        DisposableBean {

    private volatile boolean isRunning = false;

    @Autowired
    private ProfileBean profileBean;

    static {
        System.out.println(" 1.--- userBean static block");
    }

    public UserBean() {
        System.out.println(" 2.--- userBean construct. this hashCode = " + this.hashCode() +
                "\r\n      因为实现了InstantiationAwareBeanPostProcessor的类需要先于其他bean被创建,提供使用");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("14.--- userBean destroy");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println(" 5.--- userBean afterPropertiesSet 属性未完成注入, profileBean = " + profileBean + ", this hashCode = " + this.hashCode());
    }

    @Override
    public boolean isAutoStartup() {
        return true;
    }

    @Override
    public void stop(Runnable callback) {
        System.out.println("15.--- userBean stop callback");
        isRunning = false;
        callback.run();
    }

    @Override
    public void start() {
        System.out.println("12.--- userBean start");
        isRunning = true;
    }

    @Override
    public void stop() {
        System.out.println("--- userBean stop");
        isRunning = false;
    }

    @Override
    public boolean isRunning() {
        return isRunning;
    }

    @Override
    public int getPhase() {
        return 0;
    }

    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        System.out.println(" 6.--- userBean postProcessBeanDefinitionRegistry 注册完成 beanDefinitionCount:" + registry.getBeanDefinitionCount());
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        System.out.println(" 7.--- userBean postProcessBeanFactory 处理beanFactory beanDefinitionCount = " + beanFactory.getBeanDefinitionCount());
    }

    @Override
    public void postProcessBeforeDestruction(Object bean, String beanName) throws BeansException {
        System.out.println("13.--- userBean postProcessBeforeDestruction bean:" + beanName);
    }

    @Override
    public boolean requiresDestruction(Object bean) {
        return true;
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("10.--- userBean postProcessBeforeInitialization :" + beanName + " 初始化前触发");
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("11.--- userBean postProcessAfterInitialization :" + beanName + " 初始化后触发");
        return bean;
    }

    @Override
    public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
        System.out.println(" 8.--- userBean postProcessBeforeInstantiation :" + beanName + " 实例化前触发,每个bean实例化时触发一次" +
                "[注意:该方法不能在自身实例化时触发,所以userBean实例化时不会触发该方法.因为实现了InstantiationAwareBeanPostProcessor的类都先于其他bean被创建]");
        return null;
    }

    @Override
    public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
        System.out.println(" 9.--- userBean postProcessAfterInstantiation :" + beanName + " 实例化后触发,每个bean触发一次" +
                "[注意:该方法不能在自身实例化时触发,所以userBean实例化时不会触发该方法.因为实现了InstantiationAwareBeanPostProcessor的类都先于其他bean被创建]");
        return false;
    }

    @Override
    public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException {
        System.out.println("--- userBean postProcessPropertyValues :" + beanName);
        return pvs;
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println(" 3.--- userBean setBeanFactory BeanDefinition count = " + ((DefaultListableBeanFactory)beanFactory).getBeanDefinitionCount());
    }

    @Override
    public void setEnvironment(Environment environment) {
        System.out.println(" 4.--- userBean setEnvironment " + Arrays.toString(environment.getDefaultProfiles()));
    }
}

启动方法

public static void main(String[] args) {
        AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext();
        annotationConfigApplicationContext.scan("com.xie.java.lifecycle");
        annotationConfigApplicationContext.refresh();
        UserBean userBean = annotationConfigApplicationContext.getBean(UserBean.class);
        System.out.println("userBean:" + userBean);

        annotationConfigApplicationContext.getBeanFactory().destroyBean(userBean);
        annotationConfigApplicationContext.stop();
    }

执行结果一目了然,方便清楚

信息: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@dc24521: startup date [Mon Dec 21 15:34:49 CST 2020]; root of context hierarchy
 1.--- userBean static block
 2.--- userBean construct. this hashCode = 109228794
      因为实现了InstantiationAwareBeanPostProcessor的类需要先于其他bean被创建,提供使用
 3.--- userBean setBeanFactory BeanDefinition count = 7
 4.--- userBean setEnvironment [default]
 5.--- userBean afterPropertiesSet 属性未完成注入, profileBean = null, this hashCode = 109228794
 6.--- userBean postProcessBeanDefinitionRegistry 注册完成 beanDefinitionCount:7
 7.--- userBean postProcessBeanFactory 处理beanFactory beanDefinitionCount = 7
 8.--- userBean postProcessBeforeInstantiation :org.springframework.context.event.internalEventListenerProcessor 实例化前触发,每个bean实例化时触发一次[注意:该方法不能在自身实例化时触发,所以userBean实例化时不会触发该方法.因为实现了InstantiationAwareBeanPostProcessor的类都先于其他bean被创建]
 9.--- userBean postProcessAfterInstantiation :org.springframework.context.event.internalEventListenerProcessor 实例化后触发,每个bean触发一次[注意:该方法不能在自身实例化时触发,所以userBean实例化时不会触发该方法.因为实现了InstantiationAwareBeanPostProcessor的类都先于其他bean被创建]
10.--- userBean postProcessBeforeInitialization :org.springframework.context.event.internalEventListenerProcessor 初始化前触发
11.--- userBean postProcessAfterInitialization :org.springframework.context.event.internalEventListenerProcessor 初始化后触发
 8.--- userBean postProcessBeforeInstantiation :org.springframework.context.event.internalEventListenerFactory 实例化前触发,每个bean实例化时触发一次[注意:该方法不能在自身实例化时触发,所以userBean实例化时不会触发该方法.因为实现了InstantiationAwareBeanPostProcessor的类都先于其他bean被创建]
 9.--- userBean postProcessAfterInstantiation :org.springframework.context.event.internalEventListenerFactory 实例化后触发,每个bean触发一次[注意:该方法不能在自身实例化时触发,所以userBean实例化时不会触发该方法.因为实现了InstantiationAwareBeanPostProcessor的类都先于其他bean被创建]
10.--- userBean postProcessBeforeInitialization :org.springframework.context.event.internalEventListenerFactory 初始化前触发
11.--- userBean postProcessAfterInitialization :org.springframework.context.event.internalEventListenerFactory 初始化后触发
 8.--- userBean postProcessBeforeInstantiation :profileBean 实例化前触发,每个bean实例化时触发一次[注意:该方法不能在自身实例化时触发,所以userBean实例化时不会触发该方法.因为实现了InstantiationAwareBeanPostProcessor的类都先于其他bean被创建]
 9.--- userBean postProcessAfterInstantiation :profileBean 实例化后触发,每个bean触发一次[注意:该方法不能在自身实例化时触发,所以userBean实例化时不会触发该方法.因为实现了InstantiationAwareBeanPostProcessor的类都先于其他bean被创建]
10.--- userBean postProcessBeforeInitialization :profileBean 初始化前触发
11.--- userBean postProcessAfterInitialization :profileBean 初始化后触发
12月 21, 2020 3:34:49 下午 org.springframework.context.support.DefaultLifecycleProcessor start
信息: Starting beans in phase 0
12月 21, 2020 3:34:49 下午 org.springframework.context.support.DefaultLifecycleProcessor stop
信息: Stopping beans in phase 0
12.--- userBean start
userBean:com.xie.java.lifecycle.UserBean@682b2fa
13.--- userBean postProcessBeforeDestruction bean:null
14.--- userBean destroy
15.--- userBean stop callback
上一篇下一篇

猜你喜欢

热点阅读