一、概述

Spring Bean的完整生命周期从创建Spring容器开始,直到最终Spring容器销毁Bean,生命周期时序图如下:

Spring 使用介绍(十三)—— Bean的生命周期

 

二、生命周期接口分类

Bean的生命周期经历了多个接口方法的调用,这些接口和方法可分为以下四类:

1、Bean自身方法

  通过<bean>的init-method和destroy-method或注解@PostConstruct与@PreDestroy 指定的方法

2、Bean级生命周期接口

  包括BeanNameAware、BeanFactoryAware、InitializingBean、DiposableBean

// 获取bean名称
public interface BeanNameAware extends Aware {
    void setBeanName(String name);
}
// 获取BeanFactory对象
public interface BeanFactoryAware extends Aware {
    void setBeanFactory(BeanFactory beanFactory) throws BeansException;
}
// 对象初始化,与init-method属性作用相同
public interface InitializingBean {
    void afterPropertiesSet() throws Exception;
}
// 对象销毁,与destroy-method属性作用相同
public interface DisposableBean {
    void destroy() throws Exception;
}

3、容器级生命周期接口

  包括InstantiationAwareBeanPostProcessor 、BeanPostProcessor,注意:InstantiationAwareBeanPostProcessor继承自BeanPostProcessor,实际中使用InstantiationAwareBeanPostProcessorAdapter提供空实现,从而方便使用

// 对象实例化生命周期接口
public interface InstantiationAwareBeanPostProcessor extends BeanPostProcessor {
    Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException;
    boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException;
    PropertyValues postProcessPropertyValues(
            PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName)
            throws BeansException;
}
// 对象初始化生命周期接口
public interface BeanPostProcessor {
    Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException;
    Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException;
}

4、BeanFactory后处理器接口

  包括BeanFactoryPostProcessor

// 工厂后处理接口,用于修改bean配置(BeanDefinition)
public interface BeanFactoryPostProcessor {
    void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException;
}

 

三、生命周期示例演示

Bean

@Component
public class Person implements BeanFactoryAware, BeanNameAware, InitializingBean, DisposableBean {
    
    private String name;
    
    @Value("shanghai")
    private String address;
    
    @Value("123")
    private int phone;

    private BeanFactory beanFactory;
    private String beanName;

    public Person() {
        System.out.println("【构造器】调用Person的构造器实例化");
    }

    public String getName() {
        return name; 
    }

    @Autowired
    public void setName(@Value("matt") String name) {
        System.out.println("【注入属性】注入属性name");
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        System.out.println("【注入属性】注入属性address");
        this.address = address;
    }

    public int getPhone() {
        return phone;
    }

    public void setPhone(int phone) {
        System.out.println("【注入属性】注入属性phone");
        this.phone = phone;
    }

    @Override
    public String toString() {
        return "Person [address=" + address + ", name=" + name + ", phone="
                + phone + "]";
    }

    // 这是BeanFactoryAware接口方法
    @Override
    public void setBeanFactory(BeanFactory arg0) throws BeansException {
        System.out
                .println("【BeanFactoryAware接口】调用BeanFactoryAware.setBeanFactory()");
        this.beanFactory = arg0;
    }

    // 这是BeanNameAware接口方法
    @Override
    public void setBeanName(String arg0) {
        System.out.println("【BeanNameAware接口】调用BeanNameAware.setBeanName()");
        this.beanName = arg0;
    }

    // 这是InitializingBean接口方法
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out
                .println("【InitializingBean接口】调用InitializingBean.afterPropertiesSet()");
    }

    // 这是DiposibleBean接口方法
    @Override
    public void destroy() throws Exception {
        System.out.println("【DiposibleBean接口】调用DiposibleBean.destory()");
    }

    // 通过<bean>的init-method属性指定的初始化方法
    @PostConstruct
    public void myInit() {
        System.out.println("【init-method】调用<bean>的init-method属性指定的初始化方法");
    }

    // 通过<bean>的destroy-method属性指定的初始化方法
    @PreDestroy
    public void myDestory() {
        System.out.println("【destroy-method】调用<bean>的destroy-method属性指定的初始化方法");
    }
}
View Code

相关文章:

  • 2021-05-23
  • 2021-10-26
  • 2021-05-20
猜你喜欢
  • 2021-09-24
  • 2021-07-26
  • 2022-03-03
  • 2021-04-02
  • 2021-07-29
相关资源
相似解决方案