Support Bean Overrides for non-singletons

Prior to this commit, the BeanOverrideBeanFactoryPostProcessor rejected
any attempt to override a non-singleton bean; however, due to interest
from the community, we have decided to provide support for overriding
non-singleton beans via the Bean Override mechanism — for example, when
using @⁠MockitoBean, @⁠MockitoSpyBean, and @⁠TestBean.

With this commit, we now support Bean Overrides for non-singletons: for
standard JVM runtimes as well as AOT processing and AOT runtimes. This
commit also documents that non-singletons will effectively be converted
to singletons when overridden and logs a warning similar to the
following.

WARN: BeanOverrideBeanFactoryPostProcessor - Converting 'prototype' scoped bean definition 'myBean' to a singleton.

See gh-33602
See gh-32933
See gh-33800
Closes gh-35574
This commit is contained in:
Sam Brannen
2025-10-07 15:33:18 +02:00
parent ff9a349271
commit 30db2e4fb5
15 changed files with 250 additions and 53 deletions
@@ -62,8 +62,11 @@ defined by the corresponding `BeanOverrideStrategy`:
[TIP]
====
Only _singleton_ beans can be overridden. Any attempt to override a non-singleton bean
will result in an exception.
When replacing a non-singleton bean, the non-singleton bean will be replaced with a
singleton bean corresponding to bean override instance created by the applicable
`BeanOverrideHandler`, and the corresponding bean definition will be converted to a
`singleton`. Consequently, if a handler overrides a `prototype` or scoped bean, the
overridden bean will be treated as a `singleton`.
When replacing a bean created by a `FactoryBean`, the `FactoryBean` itself will be
replaced with a singleton bean corresponding to bean override instance created by the