Spring bean生命周期中的钩子

bean自身级生命周期接口

  • 配置文件中的init-method和destroy-method方法
  • bean实现BeanNameAware、BeanFactoryAware、InitializingBean和DiposableBean这些接口的方法

做个实验看一下这些方法的调用顺序

先是两个bean

package autopackage;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

public class Person implements BeanFactoryAware, BeanNameAware,InitializingBean,DisposableBean {

    private Fruit fruit;

    private String name;

    public Person(String name, Fruit fruit) {
        System.out.println("I'm the Person1");
        this.name = name;
        this.fruit = fruit;
    }

    public Person(Fruit fruit) {
        System.out.println("I'm the Person2");
        this.fruit = fruit;
    }

    public void setFruit(Fruit fruit) {
        System.out.println("I'm the setFruit");

        this.fruit = fruit;
    }

    public void setName(String name) {
        System.out.println("I'm the setName");

        this.name = name;
    }

    public void init() {
        System.out.println("I'm the init");
    }

    public void destroy1() {
        System.out.println("I'm the destroy1");
    }

    public void eat() {
        System.out.println(name+"正在吃"+fruit.getName()+"...");
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("I'm the BeanFactoryAware.setBeanFactory");
    }

    @Override
    public void setBeanName(String s) {
        System.out.println("I'm the BeanNameAware.setBeanName");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("I'm the InitializingBean.afterPropertiesSet");
    }

    @Override
    public void destroy() {
        System.out.println("I'm the DisposableBean.destroy");
    }


}
package autopackage;

public class Fruit {
    private String name;

    public Fruit(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

接着是xml配置文件




    
        
        
        
    

    
        
    


再是测试代码

package main;

import autopackage.Person;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class PersonMain {

    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("Person.xml");
        Person person = (Person) context.getBean("person");
        person.eat();
        context.registerShutdownHook();
    }
}

执行后会发现输出是这样的:

I'm the Person2
I'm the setName
I'm the BeanNameAware.setBeanName
I'm the BeanFactoryAware.setBeanFactory
I'm the InitializingBean.afterPropertiesSet
I'm the init
lkxiaolou正在吃apple...
I'm the DisposableBean.destroy
I'm the destroy1

容器级生命周期接口方法

包括了InstantiationAwareBeanPostProcessor 和 BeanPostProcessor这两个接口实现,一般称它们的实现类为“后处理器”,InstantiationAwareBeanPostProcessor 接口本质是BeanPostProcessor的子接口,我们继承Spring为其提供的适配器类InstantiationAwareBeanPostProcessor Adapter来使用它。

将上面的代码改造一下,新建一个新类

package autopackage;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;


public class PersonBeanFactoryPostProcessor implements BeanFactoryPostProcessor {

    public PersonBeanFactoryPostProcessor() {
        System.out.println("I'm the PersonBeanFactoryPostProcessor");
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
        System.out.println("I'm the PersonBeanFactoryPostProcessor.postProcessBeanFactory");
    }
}

package autopackage;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

public class PersonBeanPostProcessor implements BeanPostProcessor {

    public PersonBeanPostProcessor() {
        System.out.println("I'm the PersonBeanPostProcessor");
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof Person) {
            System.out.println("I'm the PersonBeanPostProcessor.postProcessBeforeInitialization;beanName=" + beanName);
        }
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof Person) {
            System.out.println("I'm the PersonBeanPostProcessor.postProcessAfterInitialization;beanName=" + beanName);
        }
        return bean;
    }
}

package autopackage;

import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Constructor;

public class PersonInstantiationAwareBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter {

    public PersonInstantiationAwareBeanPostProcessor() {
        super();
        System.out.println("I'm the PersonInstantiationAwareBeanPostProcessor");
    }

    @Override
    public Class predictBeanType(Class beanClass, String beanName) {
        return super.predictBeanType(beanClass, beanName);
    }

    @Override
    public Constructor[] determineCandidateConstructors(Class beanClass, String beanName) throws BeansException {
        return super.determineCandidateConstructors(beanClass, beanName);
    }

    @Override
    public Object getEarlyBeanReference(Object bean, String beanName) throws BeansException {
        return super.getEarlyBeanReference(bean, beanName);
    }

    @Override
    public Object postProcessBeforeInstantiation(Class beanClass, String beanName) throws BeansException {
        if (beanClass.equals(Person.class)) {
            System.out.println("I'm the PersonInstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation");
        }
        return super.postProcessBeforeInstantiation(beanClass, beanName);
    }

    @Override
    public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
        if (bean instanceof Person) {
            System.out.println("I'm the PersonInstantiationAwareBeanPostProcessor.postProcessAfterInstantiation");
        }
        return super.postProcessAfterInstantiation(bean, beanName);
    }

    @Override
    public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException {
        if (bean instanceof Person) {
            System.out.println("I'm the PersonInstantiationAwareBeanPostProcessor.postProcessPropertyValues");
        }
        return super.postProcessPropertyValues(pvs, pds, bean, beanName);
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof Person) {
            System.out.println("I'm the PersonInstantiationAwareBeanPostProcessor.postProcessBeforeInitialization");
        }

        return super.postProcessBeforeInitialization(bean, beanName);
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof Person) {
            System.out.println("I'm the PersonInstantiationAwareBeanPostProcessor.postProcessAfterInitialization");
        }
        return super.postProcessAfterInitialization(bean, beanName);
    }
}

xml修改一下




    
        
        
        
    

    
        
    

    
    
    




其他不变,再执行一下输出如下

I'm the PersonBeanFactoryPostProcessor
I'm the PersonBeanFactoryPostProcessor.postProcessBeanFactory
I'm the PersonInstantiationAwareBeanPostProcessor
I'm the PersonBeanPostProcessor
I'm the PersonInstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation
I'm the Person2
I'm the PersonInstantiationAwareBeanPostProcessor.postProcessAfterInstantiation
I'm the PersonInstantiationAwareBeanPostProcessor.postProcessPropertyValues
I'm the setName
I'm the BeanNameAware.setBeanName
I'm the BeanFactoryAware.setBeanFactory
I'm the PersonInstantiationAwareBeanPostProcessor.postProcessBeforeInitialization
I'm the PersonBeanPostProcessor.postProcessBeforeInitialization;beanName=person
I'm the InitializingBean.afterPropertiesSet
I'm the init
I'm the PersonInstantiationAwareBeanPostProcessor.postProcessAfterInitialization
I'm the PersonBeanPostProcessor.postProcessAfterInitialization;beanName=person
lkxiaolou正在吃apple...
I'm the DisposableBean.destroy
I'm the destroy1

总结

可以总结出整个bean的生命周期中各个钩子执行的顺序:


Spring bean生命周期中的钩子_第1张图片
bean声明周期中钩子运行顺序.jpg

你可能感兴趣的:(Spring bean生命周期中的钩子)