我是靠谱客的博主 聪明哑铃,这篇文章主要介绍《spring设计思想》12-InstantationAwareBeanPostProcessor-applyProperties之前回调postProcessProperties,现在分享给大家,希望可以做个参考。

第11节讲到InstantiationAwareBeanPostProcessor在bean被Spring“注入”属性之前将其带走了,那没被InstantiationAwareBeanPostProcessor带走的bean,接下来面对的是什么呢?

书接上文:回到AbstractAutowireCapableBeanFactory.doCreateBean(String beanName,RootBeanDefinition mbd,Object[] args)

方法:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final @Nullable Object[] args) throws BeanCreationException { // Instantiate the bean. BeanWrapper instanceWrapper = createBeanInstance(beanName, mbd, args); 。。。 // Initialize the bean instance. Object exposedObject = bean; try { //给至尊宝一个机会,否则注入beanDefinition的属性 populateBean(beanName, mbd, instanceWrapper); //初始化bean exposedObject = initializeBean(beanName, exposedObject, mbd); } 。。。 return exposedObject; }

populateBean(beanName,mbd,instanceWrapper);

这一段代码是InstantiationAwareBeanPostProcessor在执行完postProcessAfterInstantiation之后。再次执行InstantiationAwareBeanPostProcessor接口的postProcessProperties/postProcessPropertyValues两个方法:

查看接口定义

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/** * 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; } /** * 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; }

下面的postProcessPropertyValue已经废弃,推荐使用postProcessProperties方法,在给定的BeanDefinition的属性被设置到bean里面之前,可以BeanDefinition中的PropertyValues,看简单的示例:

获取到userHolder对象,进行了属性修改,descript修改成“至尊宝没带走我”

原始属性是:

最终输出结果

调用堆栈:

综上所述:在配置的BeanDefinition的propertyValues被设置到bean实例中之前,我们有机会拦截属性,并更改属性。

具体方法就是实现InstantiationAwareBeanPostProcessor的postProcessProperties

 

最后

以上就是聪明哑铃最近收集整理的关于《spring设计思想》12-InstantationAwareBeanPostProcessor-applyProperties之前回调postProcessProperties的全部内容,更多相关《spring设计思想》12-InstantationAwareBeanPostProcessor-applyProperties之前回调postProcessProperties内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(48)

评论列表共有 0 条评论

立即
投稿
返回
顶部