加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

spring中BeanPostProcessor之一:InstantiationAwareBeanPostPro

发布时间:2020-12-15 01:14:36 所属栏目:大数据 来源:网络整理
导读:前面介绍了InstantiationAwareBeanPostProcessor后置处理器的postProcessBeforeInstantiation和postProcessAfterInstantiation两个方法的用法和使用场景等。在InstantiationAwareBeanPostProcessor中还有两个方法,分别是postProcessProperties和postProcess

前面介绍了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 { pvs} otherwise.
     * In a future version of this interface (with { #postProcessPropertyValues} removed),* the default implementation will return the given { pvs} as-is directly.
     * @param pvs the property values that the factory is about to apply (never { null})
     *  bean the bean instance created,but whose properties have not yet been set
     *  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 { null} which proceeds with the existing properties
     * but specifically continues with a call to { #postProcessPropertyValues}
     * (requiring initialized { 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 { pvs} as-is.
     *  pds the relevant property descriptors for the target bean (with ignored
     * dependency types - which the factory handles specifically - already filtered out)
     *  null} to skip property population
     *  #postProcessProperties
     *  org.springframework.beans.MutablePropertyValues
     * @deprecated as of 5.1,in favor of { #postProcessProperties(PropertyValues,Object,String)}
     
    @Deprecated
    @Nullable
     PropertyValues postProcessPropertyValues(
            PropertyValues pvs,PropertyDescriptor[] pds,String beanName) return pvs;
    }

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

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

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

三、使用场合

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读