Spring Bean 生命周期整理

在这里插入图片描述

主要流程

在这里插入图片描述
在这里插入图片描述

各种接口方法分类

Bean的完整生命周期经历了各种方法调用,这些方法可以划分为以下几类:

  1. Bean自身的方法这个包括了Bean本身调用的方法和通过配置文件中<bean>init-methoddestroy-method指定的方法

  2. Bean级生命周期接口方法:这个包括了BeanNameAwareBeanFactoryAwareInitializingBeanDiposableBean这些接口的方法

  3. 容器级生命周期接口方法:这个包括了InstantiationAwareBeanPostProcessorBeanPostProcessor 这两个接口实现,一般称它们的实现类为“后处理器”。

  4. 工厂后处理器接口方法:这个包括了AspectJWeavingEnabler, ConfigurationClassPostProcessor, CustomAutowireConfigurer等等非常有用的工厂后处理器接口的方法。工厂后处理器也是容器级的。在应用上下文装配配置文件之后立即调用。

  5. BeanFactoryPostProcessor接口与BeanPostProcessor接口的作用范围是整个上下文环境中,使用方法是单独新增一个类来实现这些接口,那么在处理其他Bean的某些时刻就会回调响应的接口中的方法。

  6. BeanNameAwareBeanFactoryAwareApplicationContextAware的作用范围的Bean范围,即仅仅对实现了该接口的指定Bean有效,所有其使用方法是在要使用该功能的Bean自己来实现该接口。

演示

1、首先是一个简单的Spring Bean,调用Bean自身的方法和Bean级生命周期接口方法,为了方便演示,它实现了BeanNameAwareApplicationContextAwareBeanFactoryAwareInitializingBeanDiposableBean这5个接口,同时有2个方法,对应配置文件中<bean>init-methoddestroy-method。如下:

@Component
public class BizPerson implements Person, BeanNameAware, BeanFactoryAware, ApplicationContextAware, InitializingBean, DisposableBean {
    
    
    private Animal animal;

    @Override
    public void service() {
    
    
        animal.use();
    }

    @Override
    @Autowired
    @Qualifier("cat")
    public void setAnimal(Animal animal) {
    
    
        System.out.println("延迟依赖注入");
        this.animal = animal;
    }

    @Override
    public void setBeanName(String name) {
    
    
        System.out.println(this.getClass().getSimpleName() + "调用BeanNameAware的setBeanName方法");
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
    
    
        System.out.println(this.getClass().getSimpleName() + "调用BeanFactoryAware的setBeanFactory方法");
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    
    
        System.out.println(this.getClass().getSimpleName() + "调用ApplicationContextAware的setApplicationContext方法");
    }

    @PostConstruct
    public void init() {
    
    
        System.out.println(this.getClass().getSimpleName() + "调用@PostConstruct自定义的初始化方法");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
    
    
        System.out.println(this.getClass().getSimpleName() + "调用InitializingBean的afterPropertiesSet方法");
    }

    @PreDestroy
    public void destroy1() {
    
    
        System.out.println(this.getClass().getSimpleName() + "调用@PreDestroy自定义的销毁方法");
    }

    @Override
    public void destroy() throws Exception {
    
    
        System.out.println(this.getClass().getSimpleName() + "调用DisposableBean的destroy方法");
    }
}
public interface Animal {
    
    
    void use();
}

@Component
public class Cat implements Animal, BeanNameAware, BeanFactoryAware, ApplicationContextAware, InitializingBean, DisposableBean {
    
    
    @Override
    public void use() {
    
    
        System.out.println("猫【" + Dog.class.getSimpleName() + " 】是用来抓老鼠的。");
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
    
    
        System.out.println(this.getClass().getSimpleName() + "调用BeanFactoryAware的setBeanFactory方法");
    }

    @Override
    public void setBeanName(String s) {
    
    
        System.out.println(this.getClass().getSimpleName() + "调用BeanNameAware的setBeanName方法");
    }

    @Override
    public void destroy() throws Exception {
    
    
        System.out.println(this.getClass().getSimpleName() + "调用DisposableBean的destroy方法");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
    
    
        System.out.println(this.getClass().getSimpleName() + "调用InitializingBean的afterPropertiesSet方法");
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    
    
        System.out.println(this.getClass().getSimpleName() + "调用ApplicationContextAware的setApplicationContext方法");
    }

    @PostConstruct
    public void init() {
    
    
        System.out.println(this.getClass().getSimpleName() + "调用@PostConstruct自定义的初始化方法");
    }

    @PreDestroy
    public void destroy1() {
    
    
        System.out.println(this.getClass().getSimpleName() + "调用@PreDestroy自定义的销毁方法");
    }
}

@Component
@Primary
public class Dog implements Animal {
    
    
    @Override
    public void use() {
    
    
        System.out.println("狗【" + Dog.class.getSimpleName() + " 】是看门用的。");
    }
}
@Configuration
@ComponentScan(lazyInit = true)
public class AppConfig {
    
    
    public static void main(String[] args) {
    
    
        AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
        BizPerson bean = annotationConfigApplicationContext.getBean(BizPerson.class);
        bean.service();
        annotationConfigApplicationContext.registerShutdownHook();
    }
}

结果如下:
在这里插入图片描述

  1. 接下来是演示BeanPostProcessor接口的方法,如下:
@Component
public class MyBeanPostProcessor implements BeanPostProcessor {
    
    
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
    
    
        System.out.println("MyBeanPostProcessor.postProcessBeforeInitialization()--before->"+"初始化beanName:"+beanName);
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    
    
        System.out.println("MyBeanPostProcessor.postProcessAfterInitialization()--after-->"+"初始化beanName:"+beanName);
        return bean;
    }
}

执行结果:

在这里插入图片描述

如上,BeanPostProcessor接口包括2个方法postProcessAfterInitializationpostProcessBeforeInitialization,这两个方法的第一个参数都是要处理的Bean对象,第二个参数都是Bean的name。返回值也都是要处理的Bean对象。

  1. InstantiationAwareBeanPostProcessor 接口本质是BeanPostProcessor的子接口,一般我们继承Spring为其提供的适配器类InstantiationAwareBeanPostProcessor Adapter来使用它,如下:
@Component
public class MyInstantiationAwareBeanPostProcessor extends
        InstantiationAwareBeanPostProcessorAdapter {
    
    

    public MyInstantiationAwareBeanPostProcessor() {
    
    
        super();
        System.out.println("这是InstantiationAwareBeanPostProcessorAdapter实现类构造器!!");
    }

    // 接口方法、实例化Bean之前调用
    @Override
    public Object postProcessBeforeInstantiation(Class beanClass,
                                                 String beanName) throws BeansException {
    
    
        System.out.println("InstantiationAwareBeanPostProcessor调用postProcessBeforeInstantiation方法");
        return null;
    }

    // 接口方法、实例化Bean之后调用
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName)
            throws BeansException {
    
    
        System.out.println("InstantiationAwareBeanPostProcessor调用postProcessAfterInitialization方法");
        return bean;
    }

    // 接口方法、设置某个属性时调用
    @Override
    public PropertyValues postProcessPropertyValues(PropertyValues pvs,
                                                    PropertyDescriptor[] pds, Object bean, String beanName)
            throws BeansException {
    
    
        System.out.println("InstantiationAwareBeanPostProcessor调用postProcessPropertyValues方法");
        return pvs;
    }
}

结果如下:

在这里插入图片描述

其中第二个方法postProcessAfterInitialization就是重写了BeanPostProcessor的方法。postProcessPropertyValues用来操作属性,返回值也应该是PropertyValues对象。

  1. 演示工厂后处理器接口方法,如下
@Component
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
    
    

    public MyBeanFactoryPostProcessor() {
    
    
        super();
        System.out.println("这是BeanFactoryPostProcessor实现类构造器!!");
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory arg0)
            throws BeansException {
    
    
        System.out
                .println("BeanFactoryPostProcessor调用postProcessBeanFactory方法");
        BeanDefinition bd = arg0.getBeanDefinition("cat");
        bd.getPropertyValues().addPropertyValue("name", "110");
    }

}

在这里插入图片描述

参考

Guess you like

Origin blog.csdn.net/qq_37362891/article/details/119748450