写博客,写博客,把自己知道的小知识点全部记录,?

BeanDefinitionRegistryPostProcessor 接口属于Beanddefination  装配定义的范畴,此时bean 并没有初始化

BeanPostProcessor属于be an 实例化修改的范畴,be an 已经进行实例化,只不过我们可以修改这个be an 的实例化两个接口所对应的对象不一样

研究过Mybatis 源码的童鞋, org.mybatis.spring.mapper.MapperScannerConfigurer 扫描接口的这个类实现了BeanDefinitionRegistryPostProcessor 这个接口

BeanDefinitionRegistryPostProcessor :是Spring提供的BeanDefinaion 后置工厂处理器,可以进行Beandefination 的添加修改删除的操作;接下来我们看一下

 1. 如何进行Beandefination 的添加,修改,删除操作

 2.  Spring 哪里进行BeanDefinitionRegistryPostProcessor 后置处理器的调用

添加Beandefination 操作:

@Service
public class BeanPostDefination implements BeanDefinitionRegistryPostProcessor {@Overridepublic void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {int before = registry.getBeanDefinitionCount();System.out.println("后置处理之前的bean个数"+before);//bean的定义GenericBeanDefinition beanDefinition =new GenericBeanDefinition();//设置bean class对象交给BeanFactory 进行创建beanDefinition.setBeanClass(UserModel.class);//也可以给置顶bean 进行属性的添加,底层是一个arraylistbeanDefinition.getPropertyValues().addPropertyValue("name", "cys");//注册到bean工厂中将beanregistry.registerBeanDefinition("userModel", beanDefinition);System.out.println("后置处理之后的bean个数"+registry.getBeanDefinitionCount());}@Overridepublic void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {//nothing
    }
}

测试:

 public static void main(String[] args) throws Exception {ClassPathXmlApplicationContext context =new ClassPathXmlApplicationContext("classpath:spring/whoareyou.xml");UserModel userModel = (UserModel)context.getBean("userModel");System.out.println(userModel.getName());}

打印结果: be an的个数增加,并且能够得到我们添加的属性

删除操作:

 //删除操作根据名字registry.removeBeanDefinition("xxx");

OK,那么,这个处理器在何时被调用呢?追踪源码:有基础的童鞋应该很熟悉这个refresh()这个方法吧

点进去,里面没有复杂的逻辑,进行判断然后实例化,调用

这样Spring 的冰山一角就就完事了,

我们再看一下 BeanPostProcessor 这个后置处理器, BeanPostProcessor处理器的作用在be an 初始化实例化的范畴,可以进行be an 实例修改,比如AOP 生成cglib动态代理

而 BeanDefinitionRegistryPostProcessor 接口属于Beanddefination  装配定义的范畴

我们看一下调用他们的方法:

我们看一下,在哪里进行调用的:

源码很明白 了,根本用不上我们测试,哪个执行在前面哪个执行在后面:

就是 Bean 实例化-->AutoWrized 属性的注入--> 执行Aware 接口的方法-->执行BeanPostProcessor 的前置方法 postProcessBeforeInitialization -->

执行 InitalizingBean 的 afterPropertiesSet 方法 -->执行BeanPostProcessor 的后置方法 postProcessAfterInitialization

转载于:https://www.cnblogs.com/iscys/p/10502826.html

Spring BeanDefinitionRegistryPostProcessor BeanPostProcessor作用相关推荐

  1. 【Spring】Spring中BeanPostProcessor

    1.概述 转载:Spring中BeanPostProcessor 对文章:SpringBoot : 定制化Bean的利器:BeanPostProcessor & BeanFactoryPost ...

  2. Spring - BeanDefinitionRegistryPostProcessor 扩展接口 动态注册bean

    文章目录 Pre org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor 接口的继承关系 BeanD ...

  3. 描述一下Spring框架的作用和优点?

    Spring框架的作用和优点如下: 1.Spring是一个开源的轻量级的应用开发框架,其目的是用于简化企业级应用程序开发,减少侵入: 2.Spring提供的IOC和AOP应用,可以将组建的耦合度降低至 ...

  4. spring 注释的作用_Spring的@Primary注释在起作用

    spring 注释的作用 Spring是一个永不停息的框架. 这是因为它提供了许多不同的解决方案,使我们(开发人员)无需编写数百万行代码即可完成我们的任务. 取而代之的是,我们能够以更具可读性,更标准 ...

  5. spring 之 BeanPostProcessor

    spring 之 BeanPostProcessor 粗略一看, 它有这么多实现: 可见, 它是多么基础 而重要的一个 接口啊! 它提供了两个方法: public interface BeanPost ...

  6. Spring注解@NonNull作用 Spring注解@Nullable作用 Spring NonNull 注解作用 Spring Nullable注解作用

    Spring注解@NonNull作用 Spring注解@Nullable作用 Spring NonNull 注解作用 Spring Nullable注解作用 一.概述 在看Spring源码的时候,经常 ...

  7. Spring中BeanPostProcessor

    Spring提供了很多扩展接口,BeanPostProcessor接口和InstantiationAwareBeanPostProcessor接口就是其中两个. BeanPostProcessor B ...

  8. Spring之BeanPostProcessor(后置处理器)介绍

      为了弄清楚Spring框架,我们需要分别弄清楚相关核心接口的作用,本文来介绍下BeanPostProcessor接口 BeanPostProcessor   该接口我们也叫后置处理器,作用是在Be ...

  9. JAVA spring 常用包作用详解(转)

    转载地址:https://www.cnblogs.com/Tmc-Blog/p/6093162.html <project xmlns="http://maven.apache.org ...

最新文章

  1. 这些年,还是一个人~
  2. C# 出现System.TypeInitializationException类型初始值设定项引发异常
  3. Unity3D热更新全书-脚本(二) 两级分化
  4. mysql 数据类型详解_MySQL笔记之数据类型详解
  5. 【正一专栏】内马尔请不要把球迷的爱当做你交易的筹码
  6. Help:立体图绘制以及根据X,Y,Z三坐标值,在图上描点
  7. boost::enable_shared_from_this相关的测试程序
  8. Python学习(四) —— 编码
  9. Linux 安装json神器 jq
  10. JAVA获取Classpath根路径的方法
  11. 【转载】最好女孩子概率模型
  12. 云图说 | 华为云医疗智能体智联大健康:AI医学影像
  13. Linux运行脚手架vue,Linux Nodejs与vue脚手架详解
  14. 提取lbp特征java代码_特征提取算法之LBP
  15. 20155216 2016-2017-2 《Java程序设计》第三周学习总结
  16. android python .xlsx_python读写xlsx
  17. App Store审核标准
  18. STM32WL开发之LORA产品选型
  19. html鼠标滑动响应,CSS鼠标响应事件经过、移动、点击示例介绍
  20. GB28181平台对接接口详解

热门文章

  1. zabbix菜单出现问号乱码问题
  2. pthread_cond_wait()函数的详解
  3. java中的Random()注意!
  4. svn中的revert和update
  5. 你没听说过的Go语言惊人优点
  6. 降维后的高维特征的参数_高维超参数调整简介
  7. java 并发统计_java并发编程|CountDownLatch计数器
  8. memcache php mysql_PHP中的数据库二、memcache
  9. 列表组件之ListView
  10. 软件测试培训分享:软件测试岗位面试技巧有哪些?