• 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();
        }
        
    }
  • 相关阅读:
    PTA 乙级 1064 朋友数 (20分) C++
    关于Symbol.iterator 学习笔记
    JS函数传递参数是是按值传递
    使用Nginx做反向代理的配置
    动态修改svg的颜色,svg做背景色时候修改颜色
    macos停止MySQL服务
    git-flow-avh的使用过程
    appium+rf APP自动化问题七----点击图片下方的文字无法实现页面跳转
    appium+rf 问题六--appium setting和unlock在设备上重复安装
    python3报错---Error in sitecustomize; set PYTHONVERBOSE for traceback: NameError: name 'reload' is not defined
  • 原文地址:https://www.cnblogs.com/lgjlife/p/10664320.html
Copyright © 2020-2023  润新知