栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring源码解析 (后置处理器的实现与实现的顺序) (十一)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring源码解析 (后置处理器的实现与实现的顺序) (十一)

文章目录
      • 1.概述
      • 2.测试
      • 3.后置处理器的实现的顺序

1.概述

生命周期后置处理器: BeanPostProcessor, BeanFactoryPostProcessor

生命周期后置处理: InitializingBean, DisposableBean


后置处理器会传入bean, 在于改变, 而InitialzingBean在于额外处理

2.测试




这里通过实现类实现他们

BeanPostProcessor

@Component
public class MyBeanPostProcessor implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("MyBeanPostProcessor postProcessBeforeInitialization");
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("MyBeanPostProcessor postProcessAfterInitialization");
        return bean;
    }
}

InstantiationAwareBeanPostProcessor

@Component
public class MyInstantiationAwareBeanPostProcessor implements InstantiationAwareBeanPostProcessor {
    @Override
    public Object postProcessBeforeInstantiation(Class beanClass, String beanName) throws BeansException {
        System.out.println("MyInstantiationAwareBeanPostProcessor postProcessBeforeInstantiation");
        return null;
    }

    @Override
    public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
        System.out.println("MyInstantiationAwareBeanPostProcessor postProcessAfterInstantiation");
        return true;
    }

    @Override
    public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) throws BeansException {
        System.out.println("MyInstantiationAwareBeanPostProcessor postProcessProperties");
        return null;
    }
}

MergedBeanDefinitionPostProcessor

@Component
public class MyMergedBeanDefinitionPostProcessor implements MergedBeanDefinitionPostProcessor {
    @Override
    public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class beanType, String beanName) {
        System.out.println("MergedBeanDefinitionPostProcessor postProcessMergedBeanDefinition");
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("MyMergedBeanDefinitionPostProcessor postProcessBeforeInitialization" + beanName);
        return null;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("MyMergedBeanDefinitionPostProcessor postProcessAfterInitialization" + beanName);
        return null;
    }
}

SmartInstantiationAwareBeanPostProcessor

@Component
public class MySmartInstantiationAwareBeanPostProcessor implements SmartInstantiationAwareBeanPostProcessor {
    @Override
    public Class predictBeanType(Class beanClass, String beanName) throws BeansException {
        System.out.println("MySmartInstantiationAwareBeanPostProcessor predictBeanType");
        return null;
    }

    @Override
    public Constructor[] determineCandidateConstructors(Class beanClass, String beanName) throws BeansException {
        System.out.println("MySmartInstantiationAwareBeanPostProcessor determineCandidateConstructors");
        return null;
    }

    @Override
    public Object getEarlyBeanReference(Object bean, String beanName) throws BeansException {
        System.out.println("MySmartInstantiationAwareBeanPostProcessor getEarlyBeanReference");
        return bean;
    }
}

BeanFactoryPostProcessor

@Component
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        System.out.println("MyBeanFactoryPostProcessor postProcessBeanFactory");
    }

}

BeanDefinitionRegistryPostProcessor

@Component
public class MyBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {
    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        System.out.println("MyBeanDefinitionRegistryPostProcessor postProcessBeanDefinitionRegistry");
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        System.out.println("MyBeanDefinitionRegistryPostProcessor postProcessBeanFactory");
    }
}

InitializingBean

@Component
public class MyInitializingBean implements InitializingBean {
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("MyInitializingBean afterPropertiesSet");
    }
}

运行的顺序为

MyBeanDefinitionRegistryPostProcessor postProcessBeanDefinitionRegistry
MyBeanDefinitionRegistryPostProcessor postProcessBeanFactory
MyBeanFactoryPostProcessor postProcessBeanFactory
MySmartInstantiationAwareBeanPostProcessor predictBeanType
MyInstantiationAwareBeanPostProcessor postProcessBeforeInstantiation
MySmartInstantiationAwareBeanPostProcessor determineCandidateConstructors
MergedBeanDefinitionPostProcessor postProcessMergedBeanDefinition
MyInstantiationAwareBeanPostProcessor postProcessAfterInstantiation
MyInstantiationAwareBeanPostProcessor postProcessProperties
MyBeanPostProcessor postProcessBeforeInitialization
MyMergedBeanDefinitionPostProcessor postProcessBeforeInitializationmainConfig
MyInitializingBean afterPropertiesSet
MyBeanPostProcessor postProcessAfterInitialization
MyMergedBeanDefinitionPostProcessor postProcessAfterInitializationmainConfig
3.后置处理器的实现的顺序





转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/852049.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号