java 生命周期事件中的 BeanFactoryPostProcessor 和 BeanPostProcessor
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30455536/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
BeanFactoryPostProcessor and BeanPostProcessor in lifecycle events
提问by jasminum
I was trying to understand the difference between BeanFactoryPostProcessor
and BeanPostProcessor
.
我试图理解之间的差异BeanFactoryPostProcessor
和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
对 bean 定义进行操作,即在创建 bean 实例之前,它会被执行,并BeanPostProcessor
在 bean 实例化和生命周期事件被调用后被执行。
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
它不是BeanPostProcessor
Spring 生命周期事件的一部分,因为它在实例化之前被调用,而它是 Spring 生命周期事件的一部分?请验证我的理解是否正确。
回答by araknoid
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.
BeanFactoryPostProcessor
是一个接口,实现它的 bean 实际上是经历 Spring 生命周期的 bean(下面的示例),但这些 bean 不参与其他声明 bean 的生命周期。
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
}
}
}
The differences about BeanFactoryPostProcessor
and BeanPostProcessor
:
关于BeanFactoryPostProcessor
和的区别BeanPostProcessor
:
- 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
当所有 bean 定义都已加载但尚未实例化任何 bean 时,将调用bean 实现。这甚至允许覆盖或添加属性,甚至是急切初始化的 bean。这将使您可以访问您在 XML 中定义或注释的所有 bean(通过组件扫描扫描)。- 一个 bean 实现
BeanPostProcessor
对 bean(或对象)实例的操作,这意味着当 Spring IoC 容器实例化 bean 实例时,BeanPostProcessor 接口会完成它们的工作。 BeanFactoryPostProcessor
实现在 Spring 上下文启动期间在所有 bean 定义将被加载后被BeanPostProcessor
“调用”,而在 Spring IoC 容器实例化一个 bean 时被“调用”(即在所有单例的启动期间和对proptotypes 的需求)
回答by zak zak
Here is a flow diagram that might help to understand the spring bean initialisation life cycle.
这是一个流程图,可能有助于理解 spring bean 初始化生命周期。
As we can see, the implementation of theBeanFactoryPostProcessor is executed before any spring bean instantiation, contrary to the BeanPostprocessor, where the implemented method will be executed only when the bean is instantiated.
正如我们所见,BeanFactoryPostProcessor 的实现是在任何 spring bean 实例化之前执行的,这与 BeanPostprocessor 相反,其中实现的方法只会在 bean 实例化时执行。
The source image is from the Spring 5 Design Patterns Book.
源图像来自 Spring 5 Design Patterns Book。
I pick the explanation from the book:
我从书中选择解释:
After loading the bean definitions from all styles of configurations, BeanFactoryPostProcessor comes into the picture to modify the definition of some beans, and then the container instantiates the beans. Finally, BeanPostProcessor works on the beans, and it can modify and change the bean object. This is the initialization phase.
从所有样式的配置中加载bean定义后,BeanFactoryPostProcessor进入画面修改一些bean的定义,然后容器实例化bean。最后,BeanPostProcessor 作用于 bean,它可以修改和更改 bean 对象。这是初始化阶段。
回答by Venkat IndianEagle
The BeanFactoryPostProcessor
executes before bean Object
instantiation (ie at the time Applicationcontext
container is initialized)
的BeanFactoryPostProcessor
豆之前执行Object
的实例化(即,在时间Applicationcontext
容器被初始化)
BeanPostprocessor
is executed after the bean object is created, as it can be executed before init()
and after init()
.
BeanPostprocessor
在创建 bean 对象之后执行,因为它可以在 之前init()
和之后执行init()
。
回答by Sambeet
Bean Factory Post Procesor (BFPP):
豆厂后处理器(BFPP):
Used when we want to override XML / annotations, because Spring reads XML / annotations to create the beans. If you want to provide a different configuration to Spring during creation (at run time), then you need to use BFPP. Creating the internal dependency graph is a one time process.
当我们想要覆盖 XML/注解时使用,因为 Spring 读取 XML/注解来创建 bean。如果要在创建期间(运行时)为 Spring 提供不同的配置,则需要使用 BFPP。创建内部依赖图是一个一次性过程。
Bean Post Processor (BPP):
Bean 后处理器 (BPP):
The above step happened only once. It's like creating the "menu" of beans. After creating the bean, if you want to change the bean properties, you can't make any changes to the XML / annotations. Instead, you can use the BPP for bean configuration change after creation. The BPP has 2 execution areas, one before @postconstruct and one after @postconstruct.
上面的步骤只发生了一次。这就像创建 bean 的“菜单”。创建 bean 后,如果要更改 bean 属性,则不能对 XML/注释进行任何更改。相反,您可以在创建后使用 BPP 进行 bean 配置更改。BPP 有 2 个执行区,一个在@postconstruct 之前,一个在@postconstruct 之后。
Real Time Example:
实时示例:
You want to place an online food order from Zomato. While ordering online, you give a list of food (XML/annotations) to the restaurant. But, just before the restaurant starts making the food, you call them and ask them to change the dish (BFPP). Now the food is ready to be delivered and you've received it (Bean is created). But you want to make some modifications (like salt or chilly powder), and you can do this before tasting the food (because you know restaurants never put enough salt), or even after tasting the food (this is before and after @postconstruct). Once the taste is good, then the food is ready (the bean is ready to use).
您想从 Zomato 在线订购食品。在线订购时,您向餐厅提供一份食物清单(XML/注释)。但是,就在餐厅开始制作食物之前,您打电话给他们并要求他们换菜 (BFPP)。现在食物已准备好交付并且您已收到它(Bean 已创建)。但是你想要做一些修改(比如盐或冷粉),你可以在品尝食物之前进行(因为你知道餐馆从来没有放足够的盐),甚至在品尝食物之后(这是在@postconstruct之前和之后) . 一旦味道好,那么食物就准备好了(豆子就可以使用了)。