Skip to content

Commit 9a088b8

Browse files
committed
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.
1 parent e4792aa commit 9a088b8

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

org.springframework.beans/src/main/java/org/springframework/beans/factory/config/BeanFactoryPostProcessor.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@
3131
* <p>See PropertyResourceConfigurer and its concrete implementations
3232
* for out-of-the-box solutions that address such configuration needs.
3333
*
34+
* <p>A BeanFactoryPostProcessor may interact with and modify bean
35+
* definitions, but never bean instances. Doing so may cause premature bean
36+
* instantiation, violating the container and causing unintended side-effects.
37+
* If bean instance interaction is required, consider implementing
38+
* {@link BeanPostProcessor} instead.
39+
*
3440
* @author Juergen Hoeller
3541
* @since 06.07.2003
3642
* @see BeanPostProcessor

spring-framework-reference/src/beans.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4149,7 +4149,13 @@ org.springframework.scripting.groovy.GroovyMessenger@272961</programlisting>
41494149
<emphasis>instances</emphasis> (the objects that are created from the
41504150
configuration metadata), then you instead need to use a
41514151
<interfacename>BeanPostProcessor</interfacename> (described above in
4152-
<xref linkend="beans-factory-extension-bpp" />.</para>
4152+
<xref linkend="beans-factory-extension-bpp"/>. While it is technically
4153+
possible to work with bean instances within a
4154+
<interfacename>BeanFactoryPostProcessor</interfacename> (e.g. using
4155+
<methodname>BeanFactory.getBean()</methodname>), doing so causes
4156+
premature bean instantiation, violating the usual containter lifecycle.
4157+
This may cause negative side effects such as bypassing bean post
4158+
processing.</para>
41534159

41544160
<para>Also, <literal>BeanFactoryPostProcessors</literal> are scoped
41554161
<emphasis>per-container</emphasis>. This is only relevant if you are

0 commit comments

Comments
 (0)