Browse Source

Revised section on custom BeanPostProcessors

Issue: SPR-17556
pull/2036/head
Juergen Hoeller 7 years ago
parent
commit
5e153e4a77
  1. 58
      src/docs/asciidoc/core/core-beans.adoc

58
src/docs/asciidoc/core/core-beans.adoc

@ -3958,10 +3958,10 @@ integration interfaces.
=== Customizing Beans by Using a `BeanPostProcessor` === Customizing Beans by Using a `BeanPostProcessor`
The `BeanPostProcessor` interface defines callback methods that you can implement to The `BeanPostProcessor` interface defines callback methods that you can implement to
provide your own (or override the container's default) instantiation logic, provide your own (or override the container's default) instantiation logic, dependency
dependency-resolution logic, and so forth. If you want to implement some custom logic resolution logic, and so forth. If you want to implement some custom logic after the
after the Spring container finishes instantiating, configuring, and initializing a bean, Spring container finishes instantiating, configuring, and initializing a bean, you can
you can plug in one or more `BeanPostProcessor` implementations. plug in one or more custom `BeanPostProcessor` implementations.
You can configure multiple `BeanPostProcessor` instances, and you can control the order You can configure multiple `BeanPostProcessor` instances, and you can control the order
in which these `BeanPostProcessor` instances execute by setting the `order` property. in which these `BeanPostProcessor` instances execute by setting the `order` property.
@ -3975,13 +3975,13 @@ registration of `BeanPostProcessor` instances>>.
[NOTE] [NOTE]
==== ====
`BeanPostProcessor` instances operate on bean (or object) instances. That is, the `BeanPostProcessor` instances operate on bean (or object) instances. That is,
Spring IoC container instantiates a bean instance and then `BeanPostProcessor` instances do the Spring IoC container instantiates a bean instance and then `BeanPostProcessor`
their work. instances do their work.
`BeanPostProcessor` instances are scoped per-container. This is relevant only if you `BeanPostProcessor` instances are scoped per-container. This is relevant only if you
use container hierarchies. If you define a `BeanPostProcessor` in one container, it use container hierarchies. If you define a `BeanPostProcessor` in one container,
post-processes only the beans in that container. In other words, beans that are it post-processes only the beans in that container. In other words, beans that are
defined in one container are not post-processed by a `BeanPostProcessor` defined in defined in one container are not post-processed by a `BeanPostProcessor` defined in
another container, even if both containers are part of the same hierarchy. another container, even if both containers are part of the same hierarchy.
@ -3994,12 +3994,12 @@ The `org.springframework.beans.factory.config.BeanPostProcessor` interface consi
exactly two callback methods. When such a class is registered as a post-processor with exactly two callback methods. When such a class is registered as a post-processor with
the container, for each bean instance that is created by the container, the the container, for each bean instance that is created by the container, the
post-processor gets a callback from the container both before container post-processor gets a callback from the container both before container
initialization methods (such as `InitializingBean.afterPropertiesSet()`, after any initialization methods (such as `InitializingBean.afterPropertiesSet()` or any
declared `init` method) are called, and after any bean initialization callbacks. declared `init` method) are called, and after any bean initialization callbacks.
The post-processor can take any action with the bean instance, including ignoring the The post-processor can take any action with the bean instance, including ignoring the
callback completely. A bean post-processor typically checks for callback interfaces, or it callback completely. A bean post-processor typically checks for callback interfaces,
may wrap a bean with a proxy. Some Spring AOP infrastructure classes are implemented as or it may wrap a bean with a proxy. Some Spring AOP infrastructure classes are
bean post-processors in order to provide proxy-wrapping logic. implemented as bean post-processors in order to provide proxy-wrapping logic.
An `ApplicationContext` automatically detects any beans that are defined in the An `ApplicationContext` automatically detects any beans that are defined in the
configuration metadata that implements the `BeanPostProcessor` interface. The configuration metadata that implements the `BeanPostProcessor` interface. The
@ -4018,27 +4018,27 @@ initialization of other beans in the context, this early type detection is criti
[[beans-factory-programmatically-registering-beanpostprocessors]] [[beans-factory-programmatically-registering-beanpostprocessors]]
.Programmatically registering `BeanPostProcessor` instances .Programmatically registering `BeanPostProcessor` instances
NOTE: While the recommended approach for `BeanPostProcessor` registration is through NOTE: While the recommended approach for `BeanPostProcessor` registration is through
`ApplicationContext` auto-detection (as described earlier), you can `ApplicationContext` auto-detection (as described earlier), you can register them
register them programmatically against a `ConfigurableBeanFactory` by using the programmatically against a `ConfigurableBeanFactory` by using the `addBeanPostProcessor`
`addBeanPostProcessor` method. This can be useful when you need to evaluate conditional method. This can be useful when you need to evaluate conditional logic before
logic before registration or even for copying bean post processors across contexts in a registration or even for copying bean post processors across contexts in a hierarchy.
hierarchy. Note, however, that `BeanPostProcessor` instances added programmatically do not Note, however, that `BeanPostProcessor` instances added programmatically do not respect
respect the `Ordered` interface. Here, it is the order of registration that the `Ordered` interface. Here, it is the order of registration that dictates the order
dictates the order of execution. Note also that `BeanPostProcessor` instances registered of execution. Note also that `BeanPostProcessor` instances registered programmatically
programmatically are always processed before those registered through auto-detection, are always processed before those registered through auto-detection, regardless of any
regardless of any explicit ordering. explicit ordering.
.`BeanPostProcessor` instances and AOP auto-proxying .`BeanPostProcessor` instances and AOP auto-proxying
[NOTE] [NOTE]
==== ====
Classes that implement the `BeanPostProcessor` interface are special and are treated Classes that implement the `BeanPostProcessor` interface are special and are treated
differently by the container. All `BeanPostProcessor` instances and beans that they directly reference differently by the container. All `BeanPostProcessor` instances and beans that they
are instantiated on startup, as part of the special startup phase of the directly reference are instantiated on startup, as part of the special startup phase
`ApplicationContext`. Next, all `BeanPostProcessor` instances are registered in a sorted fashion of the `ApplicationContext`. Next, all `BeanPostProcessor` instances are registere
and applied to all further beans in the container. Because AOP auto-proxying is in a sorted fashion and applied to all further beans in the container. Because AOP
implemented as a `BeanPostProcessor` itself, neither `BeanPostProcessor` instances nor the beans auto-proxying is implemented as a `BeanPostProcessor` itself, neither `BeanPostProcessor`
they directly reference are eligible for auto-proxying and, thus, do not have aspects instances nor the beans they directly reference are eligible for auto-proxying and,
woven into them. thus, do not have aspects woven into them.
For any such bean, you should see an informational log message: `Bean someBean is not For any such bean, you should see an informational log message: `Bean someBean is not
eligible for getting processed by all BeanPostProcessor interfaces (for example: not eligible for getting processed by all BeanPostProcessor interfaces (for example: not

Loading…
Cancel
Save