究竟是如何运作的的Spring bean后处理器? [英] How exactly works the Spring bean post processor?

查看:1619
本文介绍了究竟是如何运作的的Spring bean后处理器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学习为Spring核心认证的我有春天如何处理在豆生命周期有些疑惑,特别是关于在 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.

所以我有这样的模式:

这是pretty明确对我来说意味着什么:

It is pretty clear for me what it means:

加载bean定义阶段会发生的:


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

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

添加到Bean工厂(它的ID下的每个索引)豆定义

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

特别的的BeanFactoryPostProcessor ​​豆类调用时,它可以修改任何bean的定义(例如对于财产占位符值替换)。

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

然后在矿井的豆创建阶段恰巧:


  • 每个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)

好吧,这是pretty清楚,我和我也知道,有两种类型的bean后置处理器的是:

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


  • 初始值:如果指示初始化豆(即@PostConstruct)

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

所有其余的:,允许额外的配置和可以初始化步骤之前或之后运行

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

和我张贴这张幻灯片:

所以这对我来说是很清楚哪些呢在初始化 bean后置处理器(它们是 @PostContruct 注释annoted的方法和自动二传手后立即调用方法(这样的关系是不注射后),我知道,我可以用它来执行一些初始化批次(如填充缓存在previous例子)。

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

但究竟是重新presents其他bean后处理器?什么手段,这些都是初始化阶段之前或之后进行的

But what exactly represents the other bean post processor? What means that these are performed before or after the initialization phase?

所以我的豆被实例化,并进行相关性unjection所以后来初始化阶段perfromed(由 @PostContruct annoted方法的执行)。什么意味着一个bean后置处理器的初始化阶段之前进行?这意味着,它在 @PostContruct annoted方法执行前发生的呢?所以INT意味着它可以在依赖注入发生之前(即setter方法​​被调用之前)?

So my bean are instantiated and it is performed the dependency unjection so then the initialization phase is perfromed (by the execution of a @PostContruct annoted method). What means that an Bean Post Processor is performed before the initialization phase? It means that it happen before the @PostContruct annoted method execution? So int means that it could happen before the dependency injection (before that the setter methods are called)?

和究竟意味着它的initiazalization步骤之后执行 。这意味着它之后发生在一个 @PostContruct annoted方法,或者是什么的exectuon?

And what exactly means that it is performed after the initiazalization step. It means that it happens after that the exectuon of a @PostContruct annoted method, or what?

我可以轻易猜出了我的头上,为什么我需要一个 @PostContruct annoted方法,但我想不出其他类型的bean后处理器的一些典型的例子,你能告诉我一些典型的例子均采用什么时候?

I can easily figure into my head why I need a @PostContruct annoted 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?

TNX

推荐答案

春季DOC下解释BPPS <一个href=\"http://docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html#beans-factory-extension-bpp\"相对=nofollow>自定义豆使用的BeanPostProcessor ​​。 BPP豆是一种特殊的任何其它的bean之前获得创建豆和新创建的bean相结合。通过这种结构,Spring允许你指钩行动,只是通过实施的BeanPostProcessor ​​自定义自己的生命周期行为。

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.

要undersand方法如何适应bean的生命周期,当正好方法的被调用检查<一个href=\"http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/beans/factory/config/BeanPostProcessor.html#postProcessAfterInitialization-java.lang.Object-java.lang.String-\"相对=nofollow>文档

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

postProcessBeforeInitialization(对象豆,字符串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(对象豆,字符串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 方法和Spring成为意识到这一点,当你通过寄存器 CommonAnnotationBeanPostProcessor会或指定&LT;背景:注解配置/&GT; 的bean配置文件。无论是 @PostConstruct 方法之前或之后的任何其他 postProcessAfterInitialization 取决于顺序<执行/ code>属性

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 bean后处理器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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