Spring BeanPostProcessor的工作原理是什么? [英] How exactly does the Spring BeanPostProcessor work?

查看:213
本文介绍了Spring BeanPostProcessor的工作原理是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究Spring Core认证,我对Spring如何处理 beans生命周期,尤其是 bean后处理器有疑问.

I am studying for the Spring Core certification an I have some doubts about how Spring handle the beans lifecycle and in particular about the bean post processor.

所以我有这个模式:

我很清楚这是什么意思:

It is pretty clear for me what it means:

以下步骤在 Load Bean Definitions 阶段进行:

  • 处理了 @Configuration 类和/或处理了 @Components 扫描和/或 XML文件被解析.

  • The @Configuration classes are processed and/or @Components are scanned for and/or XML files are parsed.

已将Bean定义添加到BeanFactory中(每个索引都在其ID下进行了索引)

Bean definitions added to BeanFactory (each indexed under its id)

特殊的 BeanFactoryPostProcessor Bean,它可以修改任何Bean的定义(例如,用于替换属性-占位符的值).

Special BeanFactoryPostProcessor beans invoked, it can modify the definition of any bean (for example for the property-placeholder values replacements).

然后在 beans创建阶段中执行以下步骤:

Then the following steps take place in the beans creation phase:

  • 默认情况下,每个Bean都会急切实例化(以正确的顺序创建并注入了依赖项).

  • Each bean is eagerly instantiated by default (created in right order with its dependencies injected).

在注入依赖项之后,每个bean都要进行后处理

After dependency injection each bean goes through a post-processing phase in which further configuration and initialization may occur.

在后期处理之后,bean已完全初始化并可以使用(通过其id进行跟踪,直到上下文被销毁为止)

After post processing the bean is fully initialized and ready for use (tracked by its id until the context is destroyed)

好吧,这对我来说很清楚,我也知道有两种类型的Bean后处理器:

Ok, this is pretty clear for me and I also know that there are two types of bean post processors which are:

  • 初始化器:如果得到指示,则初始化Bean(即@PostConstruct).

  • Initializers: Initialize the bean if instructed (i.e. @PostConstruct).

其余所有:允许进行其他配置,并且可以在初始化步骤之前或之后运行

and All the rest: that allow for additional configuration and that may run before or after the initialize step

然后我发布这张幻灯片:

And I post this slide:

因此,对于我来说,很清楚 initializers bean后处理器是什么(它们是用 @PostContruct 注释的方法,在设置器之后会立即自动调用它们)方法(因此在依赖项注入之后),而且我知道我可以用来执行一些初始化批处理(如上例中那样填充缓存).

So it is very clear for me what does the initializers bean post processors (they are the methods annotated with @PostContruct annotation and that are automatically called immediately after the setter methods (so after the dependency injection), and I know that I can use to perform some initialization batch (as populate a cache as in the previous example).

但是到底是什么代表了另一个bean后处理器呢?当我们说这些步骤在初始化阶段之前或之后执行是什么意思?

But what exactly represents the other bean post processor? What do we mean when we say that these steps are performed before or after the initialization phase?

因此实例化了我的bean并注入了其依赖项,因此初始化阶段完成了(通过执行 @PostContruct 带注释的方法).在初始化阶段之前使用Bean Post Processor是什么意思?这意味着它是在 @PostContruct 带注释的方法执行之前发生的吗?这是否意味着它可能发生在依赖项注入之前(在调用setter方法之前)?

So my beans are instantiated and its dependencies are injected, so then the initialization phase is completed (by the execution of a @PostContruct annotated method). What do we mean by saying that a Bean Post Processor is used before the initialization phase? It means that it happens before the @PostContruct annotated method execution? Does it means that it could happen before the dependency injection (before that the setter methods are called)?

当我们说在初始化步骤后执行时,这是什么意思?这意味着它会在执行 @PostContruct 带注释的方法之后发生,还是什么?

And what exactly do we mean when we say that it is performed after the initialization step. It means that it happens after that the execution of a @PostContruct annotated method, or what?

我可以很容易地想到为什么我需要一个 @PostContruct 注释的方法,但是我无法弄清楚另一种Bean后处理器的典型示例,您能告诉我一些典型的示例吗?何时使用?

I can easily figure into my head why I need a @PostContruct annotated method but I can't figure some typical example of the other kind of bean post processor, can you show me some typical example of when are used?

推荐答案

Spring文档在

Spring doc explains the BPPs under Customizing beans using BeanPostProcessor. BPP beans are a special kind of beans that get created before any other beans and interact with newly created beans. With this construct, Spring gives you means to hook-up to and customize the lifecycle behavior simply by implementing a BeanPostProcessor yourself.

具有类似的自定义BPP

Having a custom BPP like

public class CustomBeanPostProcessor implements BeanPostProcessor {

    public CustomBeanPostProcessor() {
        System.out.println("0. Spring calls constructor");
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName)
            throws BeansException {
        System.out.println(bean.getClass() + "  " + beanName);
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName)
            throws BeansException {
        System.out.println(bean.getClass() + "  " + beanName);
        return bean;
    }
}

将被调用并为每个创建的bean打印出类和bean名称.

would be called and print out the class and bean name for every created bean.

要了解该方法如何适应bean的生命周期以及何时正确调用该方法,请检查

To undersand how the method fit the bean's lifecycle, and when exactly the method's get called check the docs

postProcessBeforeInitialization(对象bean,字符串beanName)应用 将此BeanPostProcessor移至任何Bean之前的给定新Bean实例 初始化回调(例如InitializingBean的afterPropertiesSet 或自定义的初始化方法).

postProcessBeforeInitialization(Object bean, String beanName) Apply this BeanPostProcessor to the given new bean instance before any bean initialization callbacks (like InitializingBean's afterPropertiesSet or a custom init-method).

postProcessAfterInitialization(对象bean,字符串beanName)应用 此BeanPostProcessor到任何bean之后的给定新bean实例 初始化回调(例如InitializingBean的afterPropertiesSet 或自定义的初始化方法).

postProcessAfterInitialization(Object bean, String beanName) Apply this BeanPostProcessor to the given new bean instance after any bean initialization callbacks (like InitializingBean's afterPropertiesSet or a custom init-method).

重要的一点也是

该bean将已经填充了属性值.

The bean will already be populated with property values.

关于与@PostConstruct的关系的注意事项,请注意,此批注是声明postProcessAfterInitialization方法的便捷方法,当您通过寄存器CommonAnnotationBeanPostProcessor或在其中指定<context:annotation-config />时,Spring会意识到这一点. bean配置文件. @PostConstruct方法是在其他任何postProcessAfterInitialization之前还是之后执行取决于order属性

For what concerns the relation with the @PostConstruct note that this annotation is a convenient way of declaring a postProcessAfterInitialization method, and Spring becomes aware of it when you either by registerCommonAnnotationBeanPostProcessor or specify the <context:annotation-config /> in bean configuration file. Whether the @PostConstruct method will execute before or after any other postProcessAfterInitialization depends on the order property

您可以配置多个BeanPostProcessor实例,并且可以 通过设置控制这些BeanPostProcessor执行的顺序 订单属性.

You can configure multiple BeanPostProcessor instances, and you can control the order in which these BeanPostProcessors execute by setting the order property.

这篇关于Spring BeanPostProcessor的工作原理是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆