BeanPostProcessor混淆 [英] BeanPostProcessor confusion

查看:118
本文介绍了BeanPostProcessor混淆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Spring中理解BeanPostProcessor,但我不明白它的作用。 BeanPostProcessor是否定义了在这些点调用的两个方法是正确的:

I am trying to understand BeanPostProcessor in Spring and I don't understand what it does. Is it correct that the BeanPostProcessor defines two methods that is called at these points:


  • 在初始化之前(init方法或afterPropertiesSet),但是实例已创建。

  • 调用init方法或afterPropertiesSet方法后

这是否正确?鉴于第118页的示例和文本,进一步令人困惑。我不认为我可以将更多内容从文本复制到问题,但是注释和那里发生的事情很难理解。

Is that correct? Given the example and text on page 118 and further is confusing. I don't think I am allowed to copy more from the text to the question but the annotations and whats happening there is hard to understand.

你应该实施吗?你想要的bean上的这个接口,或者你应该在一个通用于许多bean的bean上使用它?我看到你同时传递了对象和字符串参数。

And are you supposed to implement this interface on the beans you want or are you supposed to use this on a bean that is general for many beans? I see that you get both and object and string argument passed in.


有时,你可能会发现自己处于需要$的位置b $ b在Spring
实例化bean之前和之后立即执行其他处理。处理可以像修改bean的
一样简单,也可以像返回完全不同的对象一样复杂!
BeanPostProcessor接口有两个方法:
postProcessBeforeInitialization,它在Spring调用
之前调用任何bean初始化挂钩(例如
InitializingBean.afterPropertiesSet或init-method)和
postProcessAfterInitialization,在
初始化挂钩成功后Spring调用。

Sometimes, you may find yourself in a position where you need to performsome additional processing immediately before and after Spring instantiates the bean. The processing can be as simple as modifying the bean or as complex as returning a completely different object! The BeanPostProcessor interface has two methods: postProcessBeforeInitialization, which is called before Spring calls any bean initialization hooks (such as InitializingBean.afterPropertiesSet or the init-method), and postProcessAfterInitialization, which Spring calls after the initialization hooks succeed.

Pro Spring 2.5,第118页

Pro Spring 2.5, page 118

推荐答案

Spring为您提供了大量的后处理器,而不仅仅是 BeanPostProcessor 。此外,它们中的大多数都是由Spring本身使用的。您在此问题中提到的那个(在其实例化后)使用(如其名称所示)来发布进程bean。 Spring容器行为如下:

Spring gives you a lot of post processors, not only BeanPostProcessor. Also, most of them are used by the Spring itself. The one you mentioned in this question, is used (as its name states) to post process bean after its instantiation. Spring container behavior is as follows:


  • Spring实例化bean调用其构造函数

  • postProcessBeforeInitialization(Object bean,String beanName)被称为

  • bean初始化过程: @PostConstruct afterPropertiesSet()(由 InitializingBean 回调接口定义),自定义配置 init 方法

  • postProcessAfterInitialization(Object bean,String beanName) is叫做

  • Spring instantiates bean calling its constructor
  • postProcessBeforeInitialization(Object bean, String beanName) is called
  • bean initialization process: @PostConstruct, afterPropertiesSet() (defined by the InitializingBean callback interface), custom configured init method
  • postProcessAfterInitialization(Object bean, String beanName) is called

乍一看,它可能看起来很复杂而且势不可挡,但是当你在Spring的顶层构建复杂的应用程序时,所有这些功能非常宝贵。

At the first sight, it may look complicated and overwhelming, but when you build complex applications on a top of Spring, all these features are just invaluable.

可能的情况,例如(取自Spring本身):

Possible scenarios, are for example (taken from Spring itself):


  • AutowiredAnnotationBeanPostProcessor - 扫描bean寻找
    @Autowire 注释以执行依赖注册

  • RequiredAnnotationBeanPostProcessor - 检查所有依赖项
    是否标记为 @Required 已经注入。

  • ServletContextAwareProcessor - 将 ServletContext 注入bean
    实现 ServletContextAware 接口

  • 实际上,初始化/解构回调如JSR-250 @PostConstruct @PreDestroy 使用后处理器实现: CommonAnnotationBeanPostProcessor

  • AutowiredAnnotationBeanPostProcessor - scans bean looking for @Autowire annotation in order to perform dependency injection
  • RequiredAnnotationBeanPostProcessor - checks if all dependencies marked as @Required has been injected.
  • ServletContextAwareProcessor - injects ServletContext to beans implementing ServletContextAware interface
  • actually, initialization/desctruction callbacks such as JSR-250 @PostConstruct and @PreDestroy are implemented using post processor: CommonAnnotationBeanPostProcessor

当然,所有提到的后处理器必须按特定顺序执行,但这是Spring负责确保它。

Of course all mentioned post processors must be executed in a specific order, but this is Spring responsibility to ensure it.

这篇关于BeanPostProcessor混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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