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

一、概述

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

二、详述

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

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

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

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

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

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

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

三、使用场合

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

最新文章

  1. 动态执行python脚本
  2. W5500 keep-alive的用途与用法--新华龙电子
  3. [LeetCode] Gas Station
  4. UIScrollView滚动视图
  5. SLF4J日志门面
  6. Jersey(1.19.1) - Use of @Context
  7. poj1026Cipher(置换群)
  8. poj 3894 System Engineer (二分图最大匹配--匈牙利算法)
  9. JS 操作日期
  10. Python将html转化为pdf
  11. python做数据驱动
  12. SSM框架的搭建和测试(Spring+Spring MVC+MyBatis)
  13. gitlab备份还原
  14. SOA、SOAP、RFC、RPC、IETF
  15. 创建Web API并使用
  16. CrackMe005-下篇 | 逆向破解分析 | 160个CrackMe(视频+图文)深度解析系列
  17. Spark RDD Transformation 简单用例(三)
  18. Linux切换工作目录命令:cd
  19. css翻译名词术语
  20. [Algorithm] Check for balanced parentheses using stack

热门文章

  1. 开篇词The Start以及[Vjudge][HDU2242]空调教室
  2. 单片机基础——使用GPIO扫描检测按键
  3. Lucene查询语法汇总
  4. fastdfs的入门到精通(引言和单机安装)
  5. LeetCode-最长回文串
  6. css3系列-2.css中常见的样式属性和值
  7. vue后台管理系统权限处理
  8. 开源项目OEIP 游戏引擎与音视频多媒体(UE4/Unity3D)
  9. CBV和APIView源码分析
  10. 嘉泽 P2120: 【基础】半质数 题解