Spring Bean 生命周期测试

 本文代码GitHub地址

Bean的生命周期是开始创建到销毁的过程。需要实现相关的类BeanNameAware   ,DisposableBean, InitializingBean ,并注册InstantiationAwareBeanPostProcessor。

 

 

Bean类实现BeanNameAware   ,DisposableBean, InitializingBean 接口

package com.bean.life.entity;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
import org.springframework.stereotype.Component;


@Component
public class User implements BeanFactoryAware
    , BeanNameAware
    , InitializingBean
    , DisposableBean
{
    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("BeanFactoryAware  setBeanFactory");
    }

    @Override
    public void setBeanName(String s) {
        System.out.println("BeanNameAware接口:  setBeanName = " + s);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("InitializingBean接口:  afterPropertiesSet");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("DisposableBean接口:  destroy");

    }
@PostConstruct public void init(){ System.out.println("PostConstruct"); } @PreDestroy public void destory(){ System.out.println("PreDestroy"); } }

 注册InstantiationAwareBeanPostProcessor接口。

这里的InstantiationAwareBeanPostProcessorAdapter是 InstantiationAwareBeanPostProcessor 的子孙类。

package com.bean.life.entity;

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

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


@Component
public class MyInstantiationAwareBeanPostProcessor extends
        InstantiationAwareBeanPostProcessorAdapter {


    public MyInstantiationAwareBeanPostProcessor() {
        super();
    }

    @Override
    public Constructor<?>[] determineCandidateConstructors(Class<?> beanClass, String beanName) throws BeansException {

        if(beanName.equals("user")){
            System.out.println("MyInstantiationAwareBeanPostProcessor接口: determineCandidateConstructors: " + beanName);
        }

        return super.determineCandidateConstructors(beanClass, beanName);
    }

    @Override
    public Object getEarlyBeanReference(Object bean, String beanName) throws BeansException {
        if(beanName.equals("user")){
            System.out.println("MyInstantiationAwareBeanPostProcessor接口: getEarlyBeanReference: " + beanName);
        }

        return super.getEarlyBeanReference(bean, beanName);
    }

    @Override
    public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
        if(beanName.equals("user")){
            System.out.println("MyInstantiationAwareBeanPostProcessor接口: postProcessBeforeInstantiation: " + beanName);
        }

        return super.postProcessBeforeInstantiation(beanClass, beanName);
    }

    @Override
    public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
        if(beanName.equals("user")){
            System.out.println("MyInstantiationAwareBeanPostProcessor接口: postProcessAfterInstantiation : " + beanName);
        }

        return super.postProcessAfterInstantiation(bean, beanName);
    }

    @Override
    public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException {

        if(beanName.equals("user")){
            System.out.println("MyInstantiationAwareBeanPostProcessor接口:  postProcessPropertyValues: " + beanName);
        }
        return super.postProcessPropertyValues(pvs, pds, bean, beanName);
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if(beanName.equals("user")){
            System.out.println("MyInstantiationAwareBeanPostProcessor接口: postProcessBeforeInitialization: " + beanName);
        }

        return super.postProcessBeforeInitialization(bean, beanName);
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if(beanName.equals("user")){
            System.out.println("MyInstantiationAwareBeanPostProcessor接口:  postProcessAfterInitialization: " + beanName);
        }

        return super.postProcessAfterInitialization(bean, beanName);
    }
}

 

输出

MyBeanFactoryPostProcessor ---  BeanFactoryPostProcessor
MyInstantiationAwareBeanPostProcessor接口: postProcessBeforeInstantiation: user
MyInstantiationAwareBeanPostProcessor接口: determineCandidateConstructors: user
MyInstantiationAwareBeanPostProcessor接口: postProcessAfterInstantiation : user
MyInstantiationAwareBeanPostProcessor接口:  postProcessPropertyValues: user
BeanNameAware接口:  setBeanName = user
BeanFactoryAware  setBeanFactory
MyInstantiationAwareBeanPostProcessor接口: postProcessBeforeInitialization: user
PostConstruct
InitializingBean接口:  afterPropertiesSet
MyInstantiationAwareBeanPostProcessor接口:  postProcessAfterInitialization: user

PreDestroy
DisposableBean接口:  destroy

 

 生命周期图解

Spring 实现按 init-method 和destory-method的三种方式

 

  •  方式1:xml配置方式
package com.bean.life.entity;

public class User
{

    public  void init(){
        System.out.println("PostConstruct");
    }

    public  void destory(){
        System.out.println("PreDestroy");
    }
}

 

 

<bean id="user" class="com.bean.life.entity" init-method="init" destroy-method="destroy">
</bean>

 

 

  • 方式2:注解配置

添加@PostConstruct 和@PreDestroy进行指定

package com.bean.life.entity;


@Component
public class User
{

    @PostConstruct
    public  void init(){
        System.out.println("PostConstruct");
    }

    @PreDestroy
    public  void destory(){
        System.out.println("PreDestroy");
    }
}

 

  • 方式3: 使用注解@Bean
public @interface Bean {
    @AliasFor("name")
    String[] value() default {};

    @AliasFor("value")
    String[] name() default {};

    Autowire autowire() default Autowire.NO;

    String initMethod() default "";

    String destroyMethod() default "(inferred)";
}

 

@Configuration
public class UserConfig{
   
      @Bean(initMethod="init",destoryMethod="destory")
      public User user(){
      return new User();
    }
    
}

 

posted @ 2019-04-07 10:48  冬眠的山谷  阅读(702)  评论(0编辑  收藏  举报