生命周期事件中的BeanFactoryPostProcessor和BeanPostProcessor [英] BeanFactoryPostProcessor and BeanPostProcessor in lifecycle events
问题描述
我试图了解 BeanFactoryPostProcessor
和 BeanPostProcessor
之间的区别。
I was trying to understand the difference between BeanFactoryPostProcessor
and BeanPostProcessor
.
我知道 BeanFactoryPostProcessor
对bean定义进行操作,即在创建bean实例之前,它会被执行并且<$在实例化bean并调用生命周期事件之后执行c $ c> BeanPostProcessor 。
I understood that BeanFactoryPostProcessor
operates on bean definition i.e. before the bean instance is getting created it gets executed and BeanPostProcessor
gets executed after bean is instantiated and lifecycle events are called.
这是否意味着 BeanFactoryPostProcessor
不是Spring生命周期事件的一部分,因为它在实例化之前被调用,而 BeanPostProcessor
是Spring生命周期事件的一部分吗?请核实我的理解是否正确。
Does this mean BeanFactoryPostProcessor
is not a part of spring lifecycle events as it's called before instantiation while BeanPostProcessor
is the part of Spring lifecycle events? Kindly verify if my understanding is right.
推荐答案
BeanFactoryPostProcessor
是一个界面实现它的bean实际上是经历Spring生命周期的bean(下面的例子),但这些bean不会参与其他声明bean的生命周期。
BeanFactoryPostProcessor
is an interface and beans that implement it are actually beans that undergo the Spring lifecycle (Example below) but these beans don't take part of the other declared beans' lifecycle.
public class CustomBeanFactory implements BeanFactoryPostProcessor {
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
for (String beanName : beanFactory.getBeanDefinitionNames()) {
BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName);
// Manipulate the beanDefiniton or whatever you need to do
}
}
}
关于 BeanFactoryPostProcessor
和 BeanPostProcessor
的差异:
- 在加载所有bean定义时调用实现
BeanFactoryPostProcessor
的bean,但是还没有bean被实例化。这允许覆盖或添加属性,甚至是初始化bean。这将允许您访问已在XML中定义的所有bean或已注释的bean(通过组件扫描扫描)。 - 实现
Bean BeanPostProcessor的bean / code>对bean(或对象)实例进行操作,这意味着当Spring IoC容器实例化bean实例时,BeanPostProcessor接口会完成它们的工作。
-
BeanFactoryPostProcessor
实现在Spring上下文启动期间被调用之后,所有bean定义都将被加载,而Spring IoC中code> BeanPostProcessor 被调用容器实例化一个bean(即在所有单例的启动期间以及根据需要对一个类型进行修改)
- A bean implementing
BeanFactoryPostProcessor
is called when all bean definitions will have been loaded, but no beans will have been instantiated yet. This allows for overriding or adding properties even to eager-initializing beans. This will let you have access to all the beans that you have defined in XML or that are annotated (scanned via component-scan). - A bean implementing
BeanPostProcessor
operate on bean (or object) instances which means that when the Spring IoC container instantiates a bean instance then BeanPostProcessor interfaces do their work. BeanFactoryPostProcessor
implementations are "called" during startup of the Spring context after all bean definitions will have been loaded whileBeanPostProcessor
are "called" when the Spring IoC container instantiates a bean (i.e. during the startup for all the singleton and on demand for the proptotypes one)
这篇关于生命周期事件中的BeanFactoryPostProcessor和BeanPostProcessor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!