BeanFactoryPostProcessor
BeanFactoryPostProcessor
是一个函数式接口,里面只有一个方法:
@FunctionalInterface
public interface BeanFactoryPostProcessor {
void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException;
}
再看一下doc文档上的相关描述:
Allows for custom modification of an application context’s bean definitions, adapting the bean property values of the context’s underlying bean factory. Application contexts can auto-detect BeanFactoryPostProcessor beans in their bean definitions and apply them before any other beans get created. A BeanFactoryPostProcessor may interact with and modify bean definitions, but never bean instances. Doing so may cause premature bean instantiation, violating the container and causing unintended side-effects. If bean instance interaction is required, consider implementing BeanPostProcessor instead.
机器翻译:允许自定义修改应用程序上下文的bean定义,调整上下文的基础bean工厂的bean属性值。应用程序上下文可以在其bean定义中自动检测BeanFactoryPostProcessor bean,并在创建任何其他bean之前先创建BeanFactoryPostProcessor。BeanFactoryPostProcessor可以与bean定义交互并修改bean定义,但绝不能与bean实例交互。这样做可能会导致bean过早实例化,违反容器并导致意外的副作用。如果需要bean实例交互,请考虑实现BeanPostProcessor。实现该接口,可以允许我们的程序获取到
BeanFactory
,从而修改BeanFactory
,可以实现编程式的往Spring容器中添加Bean。
总结,也就是说,我们可以通过实现BeanFactoryPostProcessor
接口,获取BeanFactory
,操作BeanFactory
对象,修改BeanDefinition
,但不要去实例化bean。
BeanDefinitionRegistryPostProcessor
BeanDefinitionRegistryPostProcessor
是BeanFactoryPostProcessor
的子类,在父类的基础上,增加了新的方法,允许我们获取到BeanDefinitionRegistry
,从而编码动态修改BeanDefinition
。例如往BeanDefinition
中添加一个新的BeanDefinition
。
public interface BeanDefinitionRegistryPostProcessor extends BeanFactoryPostProcessor {
void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException;
}
Extension to the standard BeanFactoryPostProcessor SPI, allowing for the registration of further bean definitions before regular BeanFactoryPostProcessor detection kicks in. In particular, BeanDefinitionRegistryPostProcessor may register further bean definitions which in turn define BeanFactoryPostProcessor instances.
机器翻译:对标准BeanFactoryPostProcessor SPI的扩展,允许在进行常规BeanFactoryPostProcessor检测之前注册其他Bean定义。特别是,BeanDefinitionRegistryPostProcessor可以注册其他Bean定义,这些定义又定义了BeanFactoryPostProcessor实例。
执行时机
那么BeanFactoryPostProcessor
和BeanDefinitionRegistryPostProcessor
接口是在什么时候被回调的呢?
看过我之前文章的小伙伴不知道还记不得的,这两个接口是在AbstractApplicationContext#refresh
方法中执行到invokeBeanFactoryPostProcessors(beanFactory);
方法时被执行的。Spring的编码取名真的很艺术,方法名虽然很长,但是一看就知道在做什么。
举个例子
@Repository
public class OrderDao {
public void query() {
System.out.println("OrderDao query...");
}
}
public class OrderService {
private OrderDao orderDao;
public void setDao(OrderDao orderDao) {
this.orderDao = orderDao;
}
public void init() {
System.out.println("OrderService init...");
}
public void query() {
orderDao.query();
}
}
@Component
public class MyBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
//向Spring容器中注册OrderService
BeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition(OrderService.class)
//这里的属性名是根据setter方法
.addPropertyReference("dao", "orderDao")
.setInitMethodName("init")
.setScope(BeanDefinition.SCOPE_SINGLETON)
.getBeanDefinition();
registry.registerBeanDefinition("orderService", beanDefinition);
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
// 在这里修改orderService bean的scope为PROTOTYPE
BeanDefinition beanDefinition = beanFactory.getBeanDefinition("orderService");
beanDefinition.setScope(BeanDefinition.SCOPE_PROTOTYPE);
}
}
OrderService
通过MyBeanDefinitionRegistryPostProcessor#postProcessBeanDefinitionRegistry
注册到了容器中,又通过MyBeanDefinitionRegistryPostProcessor#postProcessBeanFactory
方法修改了其Scope。最后运行主方法:
@Configuration
@ComponentScan
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Main.class);
//能成功从容器中获取orderService,并成功调用orderService.query();方法
OrderService orderService = context.getBean(OrderService.class);
orderService.query();
OrderService orderService2 = context.getBean(OrderService.class);
//false,orderService已经不是单例
System.out.println(orderService == orderService2);
context.close();
}
}
典型应用:ConfigurationClassPostProcessor
在Spring中ConfigurationClassPostProcessor
同时实现了BeanDefinitionRegistryPostProcessor
接口和其父类接口中的方法。
ConfigurationClassPostProcessor#postProcessBeanFactory
:主要负责对Full Configuration 配置进行增强,拦截@Bean
方法来确保增强执行@Bean
方法的语义。ConfigurationClassPostProcessor#postProcessBeanDefinitionRegistry
:负责扫描我们的程序,根据程序的中Bean创建BeanDefinition
,并注册到容器中。
48 条评论
ป้ายนครราชสีมา · 2023年10月4日 上午6:36
… [Trackback]
[…] Read More Information here on that Topic: hugr.cn/2019/12/06/spring拓展点:beanfactorypostprocessor接口及其子接口/ […]
ป้ายกรุงเทพ · 2023年10月4日 上午6:52
… [Trackback]
[…] Read More Information here on that Topic: hugr.cn/2019/12/06/spring拓展点:beanfactorypostprocessor接口及其子接口/ […]
220 · 2023年10月5日 下午11:20
… [Trackback]
[…] There you will find 5702 additional Info on that Topic: hugr.cn/2019/12/06/spring拓展点:beanfactorypostprocessor接口及其子接口/ […]
สล็อตเว็บนอก · 2023年10月6日 下午12:46
… [Trackback]
[…] Here you can find 17334 additional Information to that Topic: hugr.cn/2019/12/06/spring拓展点:beanfactorypostprocessor接口及其子接口/ […]
ซิเดกร้า · 2023年10月10日 上午7:30
… [Trackback]
[…] Find More to that Topic: hugr.cn/2019/12/06/spring拓展点:beanfactorypostprocessor接口及其子接口/ […]
Führerschein kaufen · 2023年10月11日 下午4:36
… [Trackback]
[…] Read More Info here to that Topic: hugr.cn/2019/12/06/spring拓展点:beanfactorypostprocessor接口及其子接口/ […]
ทํานายฝัน · 2023年10月12日 上午6:52
… [Trackback]
[…] There you can find 74284 more Information to that Topic: hugr.cn/2019/12/06/spring拓展点:beanfactorypostprocessor接口及其子接口/ […]
Europa kazino · 2023年10月12日 下午2:07
… [Trackback]
[…] Here you can find 54070 additional Info on that Topic: hugr.cn/2019/12/06/spring拓展点:beanfactorypostprocessor接口及其子接口/ […]
Kardinal Stick · 2023年10月13日 上午8:13
… [Trackback]
[…] Read More on that Topic: hugr.cn/2019/12/06/spring拓展点:beanfactorypostprocessor接口及其子接口/ […]
เครื่องนวดหน้า · 2023年10月13日 上午9:05
… [Trackback]
[…] Information on that Topic: hugr.cn/2019/12/06/spring拓展点:beanfactorypostprocessor接口及其子接口/ […]
Blackjack · 2023年10月15日 上午6:24
… [Trackback]
[…] Read More to that Topic: hugr.cn/2019/12/06/spring拓展点:beanfactorypostprocessor接口及其子接口/ […]
hizeed · 2023年10月17日 上午6:29
… [Trackback]
[…] Find More on that Topic: hugr.cn/2019/12/06/spring拓展点:beanfactorypostprocessor接口及其子接口/ […]
ผลบอล · 2023年10月17日 上午7:07
… [Trackback]
[…] There you can find 45409 more Information on that Topic: hugr.cn/2019/12/06/spring拓展点:beanfactorypostprocessor接口及其子接口/ […]
เว็บพนันอันดับ 1 · 2023年10月18日 上午7:42
… [Trackback]
[…] Find More on that Topic: hugr.cn/2019/12/06/spring拓展点:beanfactorypostprocessor接口及其子接口/ […]
ขายเศษผ้า · 2023年10月19日 上午6:41
… [Trackback]
[…] Read More on to that Topic: hugr.cn/2019/12/06/spring拓展点:beanfactorypostprocessor接口及其子接口/ […]
คาสิโนสด · 2023年10月21日 上午6:43
… [Trackback]
[…] Information on that Topic: hugr.cn/2019/12/06/spring拓展点:beanfactorypostprocessor接口及其子接口/ […]
เสือมังกร lsm99 · 2023年10月22日 上午6:54
… [Trackback]
[…] Find More to that Topic: hugr.cn/2019/12/06/spring拓展点:beanfactorypostprocessor接口及其子接口/ […]
kaya88 · 2023年10月23日 上午2:28
… [Trackback]
[…] Read More on to that Topic: hugr.cn/2019/12/06/spring拓展点:beanfactorypostprocessor接口及其子接口/ […]
สมัคร สล็อตออนไลน์ · 2023年10月23日 上午6:42
… [Trackback]
[…] Find More on that Topic: hugr.cn/2019/12/06/spring拓展点:beanfactorypostprocessor接口及其子接口/ […]
เครดิตฟรี 100 · 2023年10月23日 上午7:40
… [Trackback]
[…] Information on that Topic: hugr.cn/2019/12/06/spring拓展点:beanfactorypostprocessor接口及其子接口/ […]
https://betwinner-online.tg/code-promo/ · 2023年10月25日 下午7:44
… [Trackback]
[…] Find More on to that Topic: hugr.cn/2019/12/06/spring拓展点:beanfactorypostprocessor接口及其子接口/ […]
betwinner CD · 2023年10月26日 上午4:04
… [Trackback]
[…] Read More Information here on that Topic: hugr.cn/2019/12/06/spring拓展点:beanfactorypostprocessor接口及其子接口/ […]
slotjili · 2023年11月3日 上午8:07
… [Trackback]
[…] Read More Information here to that Topic: hugr.cn/2019/12/06/spring拓展点:beanfactorypostprocessor接口及其子接口/ […]
แทงบอล 4 ตังค์ · 2023年11月4日 上午8:58
… [Trackback]
[…] Find More Information here on that Topic: hugr.cn/2019/12/06/spring拓展点:beanfactorypostprocessor接口及其子接口/ […]
เว็บคาสิโนออนไลน์ · 2023年11月4日 上午9:17
… [Trackback]
[…] Find More on that Topic: hugr.cn/2019/12/06/spring拓展点:beanfactorypostprocessor接口及其子接口/ […]
camouflage passport · 2023年11月5日 上午2:06
… [Trackback]
[…] Find More here to that Topic: hugr.cn/2019/12/06/spring拓展点:beanfactorypostprocessor接口及其子接口/ […]
โคเวย์ · 2023年11月5日 上午10:29
… [Trackback]
[…] Find More Info here to that Topic: hugr.cn/2019/12/06/spring拓展点:beanfactorypostprocessor接口及其子接口/ […]
เกมออนไลน์ LSM99 · 2023年11月7日 上午8:54
… [Trackback]
[…] Find More Info here to that Topic: hugr.cn/2019/12/06/spring拓展点:beanfactorypostprocessor接口及其子接口/ […]
다시보기 · 2023年11月7日 下午7:01
… [Trackback]
[…] Information to that Topic: hugr.cn/2019/12/06/spring拓展点:beanfactorypostprocessor接口及其子接口/ […]
มวยพักยก · 2023年11月9日 上午8:25
… [Trackback]
[…] Read More here on that Topic: hugr.cn/2019/12/06/spring拓展点:beanfactorypostprocessor接口及其子接口/ […]
แทงบอล lsm99 · 2023年11月10日 下午12:13
… [Trackback]
[…] Find More to that Topic: hugr.cn/2019/12/06/spring拓展点:beanfactorypostprocessor接口及其子接口/ […]
แทงหวยออนไลน์ · 2023年11月11日 下午11:57
… [Trackback]
[…] There you will find 10548 more Information to that Topic: hugr.cn/2019/12/06/spring拓展点:beanfactorypostprocessor接口及其子接口/ […]
รับทำบัญชี · 2023年11月12日 上午10:46
… [Trackback]
[…] Find More Information here to that Topic: hugr.cn/2019/12/06/spring拓展点:beanfactorypostprocessor接口及其子接口/ […]
yehyeh · 2023年11月12日 上午10:57
… [Trackback]
[…] Find More Information here on that Topic: hugr.cn/2019/12/06/spring拓展点:beanfactorypostprocessor接口及其子接口/ […]
เซียน24 · 2023年11月12日 上午11:15
… [Trackback]
[…] Read More here to that Topic: hugr.cn/2019/12/06/spring拓展点:beanfactorypostprocessor接口及其子接口/ […]
bonanza178 · 2023年11月12日 下午10:59
… [Trackback]
[…] Here you will find 43245 additional Info on that Topic: hugr.cn/2019/12/06/spring拓展点:beanfactorypostprocessor接口及其子接口/ […]
cheap k2 spray on paper · 2023年11月13日 上午10:55
… [Trackback]
[…] Read More Information here on that Topic: hugr.cn/2019/12/06/spring拓展点:beanfactorypostprocessor接口及其子接口/ […]
웹툰 미리보기 · 2023年11月14日 上午8:25
… [Trackback]
[…] There you can find 31174 more Info to that Topic: hugr.cn/2019/12/06/spring拓展点:beanfactorypostprocessor接口及其子接口/ […]
polar · 2023年11月16日 上午9:24
… [Trackback]
[…] Read More here on that Topic: hugr.cn/2019/12/06/spring拓展点:beanfactorypostprocessor接口及其子接口/ […]
ทดลองเล่นสล็อต · 2023年11月16日 上午9:46
… [Trackback]
[…] Find More here on that Topic: hugr.cn/2019/12/06/spring拓展点:beanfactorypostprocessor接口及其子接口/ […]
basics · 2023年11月16日 下午10:09
… [Trackback]
[…] Info to that Topic: hugr.cn/2019/12/06/spring拓展点:beanfactorypostprocessor接口及其子接口/ […]
ข่าวบอล · 2023年11月17日 上午8:14
… [Trackback]
[…] Read More to that Topic: hugr.cn/2019/12/06/spring拓展点:beanfactorypostprocessor接口及其子接口/ […]
ทีเด็ดฟุตบอล · 2023年11月17日 上午10:16
… [Trackback]
[…] Here you will find 97354 more Info to that Topic: hugr.cn/2019/12/06/spring拓展点:beanfactorypostprocessor接口及其子接口/ […]
โปรแกรมพรีเมียร์ลีก · 2023年11月17日 上午10:37
… [Trackback]
[…] Find More on to that Topic: hugr.cn/2019/12/06/spring拓展点:beanfactorypostprocessor接口及其子接口/ […]
วิเคราะห์บอลวันนี้ · 2023年11月17日 上午10:58
… [Trackback]
[…] Read More here to that Topic: hugr.cn/2019/12/06/spring拓展点:beanfactorypostprocessor接口及其子接口/ […]
bonanza178 · 2023年11月17日 下午11:53
… [Trackback]
[…] Find More to that Topic: hugr.cn/2019/12/06/spring拓展点:beanfactorypostprocessor接口及其子接口/ […]
top article · 2023年11月18日 上午2:16
… [Trackback]
[…] Find More Info here to that Topic: hugr.cn/2019/12/06/spring拓展点:beanfactorypostprocessor接口及其子接口/ […]
superkaya88 · 2023年11月18日 下午3:39
… [Trackback]
[…] Find More here on that Topic: hugr.cn/2019/12/06/spring拓展点:beanfactorypostprocessor接口及其子接口/ […]
评论已关闭。