前面介紹了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內部已經實現好的類。