Java BeanPostProcessor与BeanFactoryPostProcessor基础使用讲解
作者:氵奄不死的鱼 时间:2022-10-25 21:52:35
前言
BeanPostProcessor 接口定义了一个你可以自己实现的回调方法,来实现你自己的实例化逻辑、依赖解决逻辑等,如果你想要在Spring完成对象实例化、配置、初始化之后实现自己的业务逻辑,你可以补充实现一个或多个BeanPostProcessor的实现。
BeanFactoryPostProcessor的定义和BeanPostProcessor相似,有一个最主要的不同是:BeanFactoryPostProcessor可以对bean的配置信息进行操作;更确切的说Spring IOC容器允许BeanFactoryPostProcessor读取配置信息并且能够在容器实例化任何其他bean(所有的实现了BeanFactoryPostProcessor接口的类)之前改变配置信息
BeanPostProcessor
接口定义
public interface BeanPostProcessor {
@Nullable
default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
@Nullable
default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
}
postProcessBeforeInitialization和postProcessAfterInitialization
入参是 bean示例和beanName,此方法内可以对bean进行处理并且返回一个对象,更改bean实例,例如代理,修改对象数据
执行时机
执行时机参考
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#initializeBean(java.lang.String, java.lang.Object, org.springframework.beans.factory.support.RootBeanDefinition)
if (mbd == null || !mbd.isSynthetic()) {
wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
}
try {
invokeInitMethods(beanName, wrappedBean, mbd);
}
catch (Throwable ex) {
throw new BeanCreationException(
(mbd != null ? mbd.getResourceDescription() : null),
beanName, "Invocation of init method failed", ex);
}
if (mbd == null || !mbd.isSynthetic()) {
wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
}
即整个bean已经加载完毕,依赖的bean已经注入完毕,分别在,执行初始化方法前和方法后执行
初始化方法指的是执行InitializingBean的afterPropertiesSet方法
初始化方法指的是bean实现了InitializingBean接口,对应的方法为afterPropertiesSet
BeanFactoryPostProcessor
接口定义
FunctionalInterface
public interface BeanFactoryPostProcessor {
void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException;
}
入参就是beanFactory,可以对beanFactory进行修改
例如通过beanFactory修改beanDefination,添加属性
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
System.out.println("调用MyBeanFactoryPostProcessor的postProcessBeanFactory");
BeanDefinition bd = beanFactory.getBeanDefinition("myJavaBean");
MutablePropertyValues pv = bd.getPropertyValues();
if (pv.contains("remark")) {
pv.addPropertyValue("remark", "在BeanFactoryPostProcessor中修改之后的备忘信息");
}
}
}
执行时机
在bean实例化之前执行,在invokeBeanFactoryPostProcessors中。
来源:https://blog.csdn.net/qq_37436172/article/details/127834066
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java基础之String类使用与字符串比较
Android编程设计模式之原型模式实例详解
![](https://img.aspxhome.com/file/2023/1/128951_0s.png)
Quartz与Spring集成的两种方法示例
简单说说Java SE、Java EE、Java ME三者之间的区别
C++智能指针实例详解
Spring Boot 集成Dubbo框架实例
Java使用Math.random()结合蒙特卡洛方法计算pi值示例
![](https://img.aspxhome.com/file/2023/6/60606_0s.jpg)
Android带进度的圆形进度条
![](https://img.aspxhome.com/file/2023/8/86748_0s.jpg)
谈一谈Android内存泄漏问题
idea首次使用需要配置哪些东西
![](https://img.aspxhome.com/file/2023/6/129956_0s.png)
Android图像处理之霓虹滤镜效果
![](https://img.aspxhome.com/file/2023/3/138993_0s.jpg)
Java Hutool工具实现验证码生成及Excel文件的导入和导出
![](https://img.aspxhome.com/file/2023/7/76507_0s.png)
非常实用的侧滑删除控件SwipeLayout
Android 文件存储及常见问题解决
Springboot内置的工具类之CollectionUtils示例讲解
![](https://img.aspxhome.com/file/2023/0/58440_0s.jpg)
使用RecyclerView实现水平列表
![](https://img.aspxhome.com/file/2023/5/137315_0s.gif)
Android自定义有限制区域图例角度自识别涂鸦工具类中篇
![](https://img.aspxhome.com/file/2023/8/138508_0s.jpg)