From 9a088b8128cb1a8027a13645a7e4d400d767aabb Mon Sep 17 00:00:00 2001 From: Chris Beams Date: Tue, 17 Aug 2010 09:50:22 +0000 Subject: [PATCH] Document BeanFactoryPostProcessor implementation constraints (SPR-7466) JavaDoc and reference docs now warn developers against interacting with bean instances within BeanFactoryPostProcessor implementations in order to avoid premature bean instantiation. See SPR-7450 for an example of a third-party BFPP (OAuthTokenLifecycleRegistryPostProcessor) prematurely instantiating a FactoryBean instance, such that bean post-processing is bypassed and autowired injection (via @Inject) never occurs. --- .../beans/factory/config/BeanFactoryPostProcessor.java | 6 ++++++ spring-framework-reference/src/beans.xml | 8 +++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/org.springframework.beans/src/main/java/org/springframework/beans/factory/config/BeanFactoryPostProcessor.java b/org.springframework.beans/src/main/java/org/springframework/beans/factory/config/BeanFactoryPostProcessor.java index c61ce8272d..1619505c1f 100644 --- a/org.springframework.beans/src/main/java/org/springframework/beans/factory/config/BeanFactoryPostProcessor.java +++ b/org.springframework.beans/src/main/java/org/springframework/beans/factory/config/BeanFactoryPostProcessor.java @@ -31,6 +31,12 @@ import org.springframework.beans.BeansException; *

See PropertyResourceConfigurer and its concrete implementations * for out-of-the-box solutions that address such configuration needs. * + *

A BeanFactoryPostProcessor may interact with and modify bean + * definitions, but never bean instances. Doing so may cause premature bean + * instantiation, violating the container and causing unintended side-effects. + * If bean instance interaction is required, consider implementing + * {@link BeanPostProcessor} instead. + * * @author Juergen Hoeller * @since 06.07.2003 * @see BeanPostProcessor diff --git a/spring-framework-reference/src/beans.xml b/spring-framework-reference/src/beans.xml index f6669f8378..dabb662d2e 100644 --- a/spring-framework-reference/src/beans.xml +++ b/spring-framework-reference/src/beans.xml @@ -4149,7 +4149,13 @@ org.springframework.scripting.groovy.GroovyMessenger@272961 instances (the objects that are created from the configuration metadata), then you instead need to use a BeanPostProcessor (described above in - . + . While it is technically + possible to work with bean instances within a + BeanFactoryPostProcessor (e.g. using + BeanFactory.getBean()), doing so causes + premature bean instantiation, violating the usual containter lifecycle. + This may cause negative side effects such as bypassing bean post + processing. Also, BeanFactoryPostProcessors are scoped per-container. This is only relevant if you are -- GitLab