前面介绍了InstantiationAwareBeanPostProcessor后置处理器的postProcessBeforeInstantiation和postProcessAfterInstantiation两个方法的用法和使用场景等。在InstantiationAwareBeanPostProcessor中还有两个方法,分别是postProcessProperties和postProcessPropertyValues,从这两个方法的名称上来看,它们完成的功能似乎很相近,下面来看具体的方法。

一、概述

在InstantiationAwarePostProcessor接口中的两个方法如下,

二、详述

在InstantiationAwareBeanPostProcessor接口中找到两个方法的定义如下,

  1. /**
  2. * Post-process the given property values before the factory applies them
  3. * to the given bean, without any need for property descriptors.
  4. * <p>Implementations should return {@code null} (the default) if they provide a custom
  5. * {@link #postProcessPropertyValues} implementation, and {@code pvs} otherwise.
  6. * In a future version of this interface (with {@link #postProcessPropertyValues} removed),
  7. * the default implementation will return the given {@code pvs} as-is directly.
  8. * @param pvs the property values that the factory is about to apply (never {@code null})
  9. * @param bean the bean instance created, but whose properties have not yet been set
  10. * @param beanName the name of the bean
  11. * @return the actual property values to apply to the given bean (can be the passed-in
  12. * PropertyValues instance), or {@code null} which proceeds with the existing properties
  13. * but specifically continues with a call to {@link #postProcessPropertyValues}
  14. * (requiring initialized {@code PropertyDescriptor}s for the current bean class)
  15. * @throws org.springframework.beans.BeansException in case of errors
  16. * @since 5.1
  17. * @see #postProcessPropertyValues
  18. */
  19. @Nullable
  20. default PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName)
  21. throws BeansException {
  22.  
  23. return null;
  24. }

上面的是postProcessProperties方法,我这里贴出了方法的注释,从注释中我们可以看出该方法从5.1版本后才有。下面看postProcessPropertyValues方法,

  1. /**
  2. * Post-process the given property values before the factory applies them
  3. * to the given bean. Allows for checking whether all dependencies have been
  4. * satisfied, for example based on a "Required" annotation on bean property setters.
  5. * <p>Also allows for replacing the property values to apply, typically through
  6. * creating a new MutablePropertyValues instance based on the original PropertyValues,
  7. * adding or removing specific values.
  8. * <p>The default implementation returns the given {@code pvs} as-is.
  9. * @param pvs the property values that the factory is about to apply (never {@code null})
  10. * @param pds the relevant property descriptors for the target bean (with ignored
  11. * dependency types - which the factory handles specifically - already filtered out)
  12. * @param bean the bean instance created, but whose properties have not yet been set
  13. * @param beanName the name of the bean
  14. * @return the actual property values to apply to the given bean (can be the passed-in
  15. * PropertyValues instance), or {@code null} to skip property population
  16. * @throws org.springframework.beans.BeansException in case of errors
  17. * @see #postProcessProperties
  18. * @see org.springframework.beans.MutablePropertyValues
  19. * @deprecated as of 5.1, in favor of {@link #postProcessProperties(PropertyValues, Object, String)}
  20. */
  21. @Deprecated
  22. @Nullable
  23. default PropertyValues postProcessPropertyValues(
  24. PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException {
  25.  
  26. return pvs;
  27. }

从上面可以看出该方法上已经被标记为@Deprecated即为废弃的,且是从5.1开始才被废弃的。也就是说从5.1开始均使用postProcessProperties方法。

既然,在5.1之后postProcessPropertyValues方法已废弃,使用的postProcessPropertiesf方法,那么他们的功能肯定是一样的,那么就只看postProcessProperties方法即可。

postProcessProperties方法要完成的工作是处理类中的属性,并为属性赋值,在spring中已经有相应的实现,CommonAnnotationBeanPostProcessor和AutowiredAnnotationBeanPostProcessor两个实现便可以完成这些工作。这里就不再列举详细的使用方式,下面会具体分析CommonAnnotationBeanPostProcessor和AutowiredAnnotationBeanPostProcessor两个实现类的具体实现,以及这两个类分别完成的功能。

三、使用场合

如果需要使用自定义的spring框架,在进行属性注入的时候不想使用spring的方式,这里可以选择实现InstantiationAwareBeanPostProcessor接口,实现postProcessProperties方法,自己实现属性注入的方式。不过最好还是使用spring内部已经实现好的类。

spring中BeanPostProcessor之一:InstantiationAwareBeanPostProcessor(03)的更多相关文章

  1. spring中BeanPostProcessor之一:InstantiationAwareBeanPostProcessor(01)

    在spring中beanPostProcessor绝对是开天辟地的产物,给了程序员很多自主权,beanPostProcessor即常说的bean后置处理器. 一.概览 先来说下Instantiatio ...

  2. Spring中BeanPostProcessor

    Spring中BeanPostProcessor 前言: 本文旨在介绍Spring动态配置数据源的方式,即对一个DataSource的配置诸如jdbcUrl,user,password,driverC ...

  3. spring中BeanPostProcessor之三:InitDestroyAnnotationBeanPostProcessor(01)

    在<spring中BeanPostProcessor之二:CommonAnnotationBeanPostProcessor(01)>一文中,分析到在调用CommonAnnotationB ...

  4. spring中BeanPostProcessor之四:AutowiredAnnotationBeanPostProcessor(01)

    在<spring中BeanPostProcessor之二:CommonAnnotationBeanPostProcessor(01)>中分析了CommonAnnotationBeanPos ...

  5. spring中BeanPostProcessor之一:InstantiationAwareBeanPostProcessor(02)

    在上篇博客中写道了bean后置处理器InstantiationAwareBeanPostProcessor,只介绍了其中一个方法的作用及用法,现在来看postProcessBeforeInstanti ...

  6. spring中BeanPostProcessor之二:CommonAnnotationBeanPostProcessor(01)

    在上篇博客中分享了InstantiationAwareBeanPostProcessor接口中的四个方法,分别对其进行了详细的介绍,在文末留下了一个问题,那就是postProcessPropertie ...

  7. spring(三):spring中BeanPostProcessor的使用

    spring中实现BeanPostProcessor的后置处理器 ApplicationContextAwareProcessor 进入该实现类内部 可以看到:该类帮我们组建IOC容器,判断我们的be ...

  8. 通过BeanPostProcessor理解Spring中Bean的生命周期

    通过BeanPostProcessor理解Spring中Bean的生命周期及AOP原理 Spring源码解析(十一)Spring扩展接口InstantiationAwareBeanPostProces ...

  9. Spring中的BeanPostProcessor

    一.何谓BeanProcessor BeanPostProcessor是SpringFramework里非常重要的核心接口之一,我先贴出一段源代码: /* * Copyright 2002-2015 ...

随机推荐

  1. utuntu sever1804显示中文putty可以输入中文

    默认情况下,putty连接ubuntu server以后,哪怕设置的Utf-8的连接,也是无法显示中文的. 应该是ubuntu服务器端,没有字库的问题. 如果在putty显示和输入中文呢,因为配置信息 ...

  2. Redis03——Redis是如何删除你的数据的

    众所周知Redis针对每一个key都能单独设置过期时间,那么Redis是怎么处理这些key的过期时间的呢?当同一时间有大量Key同时到期时,Redis又是怎么处理的呢?会不会影响到我的线上业务呢?如果 ...

  3. 写于疫情期间的一个plantUML例子

    @startuml 这几天的正经事 start repeat if(思维清晰) then (yes) :刷题; else (no) if(想写程序) then (yes) :调项目; else (no ...

  4. Python多线程的事件监控

    设想这样一个场景: 你创建了10个子线程,每个子线程分别爬一个网站,一开始所有子线程都是阻塞等待.一旦某个事件发生:例如有人在网页上点了一个按钮,或者某人在命令行输入了一个命令,10个爬虫同时开始工作 ...

  5. Git&sourceTree软件安装、使用说明及遇到问题解决

    一.软件版本 1.Git版本为1.9.5 2.Source版本为1.5.2 二.软件安装步骤 1.Git安装步骤 1)双击Git安装文件进入下图界面,单击Next 2)继续Next 3)进入Selec ...

  6. (转)C代码优化方案

    C代码优化方案 原文地址:http://www.uml.org.cn/c++/200811103.asp 目录 C代码优化方案 1.选择合适的算法和数据结构 2.使用尽量小的数据类型 3.减少运算的强 ...

  7. (转)协议森林11 涅槃 (TCP重新发送)

    协议森林11 涅槃 (TCP重新发送) 作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! TCP协议是一个可靠的协议.它通过重新发送 ...

  8. 左侧带三角的Card css支持hover阴影

    <div class="inputReportIndex"> <div class="inner"> <div class=&qu ...

  9. Quantitative Proteomics of Enriched Esophageal and Gut Tissues from the Human Blood Fluke Schistosoma mansoni Pinpoints Secreted Proteins for Vaccine Development (解读人:张聪敏)

    文献名:Quantitative Proteomics of Enriched Esophageal and Gut Tissues from the Human Blood Fluke Schist ...

  10. Mol Cell Proteomics. | Proteomics Analysis of Extracellular Matrix Remodeling During Zebrafish Heart Regeneration (解读人:徐宁)

    文献名:Proteomics Analysis of Extracellular Matrix Remodeling During Zebrafish Heart Regeneration(斑马鱼心脏 ...