• Spring的后处理器


    目录

    引言

    BeanFactoryPostProcessor

    注意 

     BeanPostProcessor


    引言

    Spring的后处理器是spring对外开发的重要扩展点,允许我们介入到Bean的整个实例化流程来,以达到动态注册BeanDefintion,动态修改BeanDefintion,以及动态修改Bean的作用。

    Spring主要有两种后处理器:

    • BeanFactoryPostProcessor:Bean工厂后处理器,BeanDefintionMap填充完毕,Bean实例化之前执行;

    • BeanPostProcessor:Bean后处理器,一般在Bean实例化之后,填充单例池singletonObjects之前执行

    BeanFactoryPostProcessor

     BeanFactoryPostProcessor是一个接口规范,实现该接口的类只要交由Spring容器管理的话,那么Spring就会回调该接口的方法,用于对BeanDefinition注册和修改的功能

     完善Bean实例化图

    1. package com.xfy.processor;
    2. import org.springframework.beans.BeansException;
    3. import org.springframework.beans.factory.config.BeanDefinition;
    4. import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
    5. import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
    6. import org.springframework.beans.factory.support.DefaultListableBeanFactory;
    7. import org.springframework.beans.factory.support.RootBeanDefinition;
    8. public class MyBeanFactorypostprocessor implements BeanFactoryPostProcessor {
    9. public void postProcessBeanFactory(ConfigurableListableBeanFactory BeanFactory) throws BeansException {
    10. System.out.println("pPBF");
    11. //修改某个BeanDifinition的全限定名,将某个Bean的类型改变
    12. BeanDefinition userDao = BeanFactory.getBeanDefinition("userDao");
    13. userDao.setBeanClassName("com.xfy.service.impl.UserServiceImpl");
    14. //动态的去注册一个BeanDefinition
    15. BeanDefinition BeanDefinition = new RootBeanDefinition();
    16. BeanDefinition.setBeanClassName("com.xfy.dao.Impl.PersonDaoImpl");
    17. DefaultListableBeanFactory DBeanFactory= (DefaultListableBeanFactory) BeanFactory;
    18. DBeanFactory.registerBeanDefinition("personDao",BeanDefinition);
    19. }
    20. }

    <bean class="com.xfy.processor.MyBeanFactorypostprocessor">bean>
    注意 

    Spring提供了一个BeanFactoryPostProcessor的子接口DBeanFactoryRegisterPostProcessor专门用于注册BeanDefintion操作    如下代码

    1. package com.xfy.processor;
    2. import org.springframework.beans.BeansException;
    3. import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
    4. import org.springframework.beans.factory.support.BeanDefinitionRegistry;
    5. import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
    6. import org.springframework.beans.factory.support.RootBeanDefinition;
    7. public class MyBeanFactorypostRegistryprocessor implements BeanDefinitionRegistryPostProcessor {
    8. public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException {
    9. RootBeanDefinition beanDefinition = new RootBeanDefinition("com.xfy.dao.Impl.PersonDaoImpl");
    10. beanDefinitionRegistry.registerBeanDefinition("personDao",beanDefinition);
    11. }
    12. public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
    13. }
    14. }

     BeanPostProcessor

    Bean被实例化后,到最终缓存到名为singletonObjects单例池之前,中间会经过Bean的初始化过程,例如:属性填充,初始化方法init的执行等,其中有一个对外进行扩展的点BeanPostProcessor。我们称为Bean后处理器,跟上面的Bean工厂后处理器相似,他也是一个接口,实现该接口并被容器管理的BeanPostProcessor,会在流程节点上被Spring自动调用

    完善Bean实例化流程图

    代码实现对Bean的属性赋值如下:

    1. package com.xfy.processor;
    2. import com.xfy.anno.MyComponent;
    3. import com.xfy.dao.Impl.UserDaoImpl;
    4. import org.springframework.beans.BeansException;
    5. import org.springframework.beans.factory.config.BeanPostProcessor;
    6. @MyComponent("mbpp")
    7. public class MyBeanPostProcessor implements BeanPostProcessor {
    8. @Override
    9. public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
    10. if (bean instanceof UserDaoImpl){
    11. UserDaoImpl userDao= (UserDaoImpl) bean;
    12. userDao.setName("haohao");
    13. }
    14. System.out.println(beanName+" postProcessBeforeInitialization");
    15. return bean;
    16. }
    17. @Override
    18. public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    19. System.out.println(beanName+" postProcessAfterInitialization");
    20. return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
    21. }
    22. }

  • 相关阅读:
    Ubuntu 20.04.4LTS 系统无法启动修复
    【uniapp】仿微信支付界面
    LeetCode 20.有效的括号
    【Shell学习笔记】Bash的模式扩展
    快速幂算法的实现
    Android 100元平板也能吃鸡玩王者!小米平板刷机神盾dot1.2保姆级教程。
    C++ 编译器中对 use after free 的检查示例
    开利网络与大学生“欹异杯”短视频创意与带货大赛达成战略合作关系
    玩链子游戏
    可观测性-Event-指标事件采样策略总结
  • 原文地址:https://blog.csdn.net/qq_60619678/article/details/134496779