Class BeanOverrideHandler
WARNING: Implementations are used as a cache key and must
implement proper equals() and hashCode() methods based on the
unique set of metadata used to identify the bean to override. Overridden
equals() and hashCode() methods should also delegate to the
super implementations in this class in order to support the basic
metadata used by all bean overrides. In addition, it is recommended that
implementations override toString() to include all relevant metadata
in order to enhance diagnostics.
Concrete implementations of BeanOverrideHandler can store additional
metadata to use during override instance
creation — for example, based on further processing of the annotation,
the annotated field, or the annotated class.
Singleton Semantics
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 handler, and the corresponding bean definition will be converted to a singleton. Consequently, if a handler overrides a prototype or custom 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 handler.
When wrapping a bean created by a
FactoryBean, the object
created by the FactoryBean will be wrapped, not the FactoryBean
itself.
- Since:
- 6.2
- Author:
- Simon Baslé, Stephane Nicoll, Sam Brannen
- See Also:
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedBeanOverrideHandler(@Nullable Field field, ResolvableType beanType, @Nullable String beanName, String contextName, BeanOverrideStrategy strategy) Construct a newBeanOverrideHandlerfrom the supplied values.protectedBeanOverrideHandler(@Nullable Field field, ResolvableType beanType, @Nullable String beanName, BeanOverrideStrategy strategy) Deprecated, for removal: This API element is subject to removal in a future version. -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract ObjectcreateOverrideInstance(String beanName, @Nullable BeanDefinition existingBeanDefinition, @Nullable Object existingBeanInstance) Create a bean override instance for an existingBeanDefinitionor an existing singleton bean, based on the metadata in thisBeanOverrideHandler.booleanstatic List<BeanOverrideHandler> forTestClass(Class<?> testClass) Process the giventestClassand build the correspondingBeanOverrideHandlerlist derived from@BeanOverridefields in the test class and its type hierarchy.Get the bean name to override, ornullto look for a single matching bean of typegetBeanType().final ResolvableTypeGet the bean type to override.final StringGet the name of the context hierarchy level in which this handler should be applied.getField()Get theFieldannotated with@BeanOverride.final BeanOverrideStrategyGet theBeanOverrideStrategyfor thisBeanOverrideHandler, which influences how and when the bean override instance should be created.inthashCode()toString()protected voidtrackOverrideInstance(Object override, SingletonBeanRegistry singletonBeanRegistry) Track the supplied bean override instance that was created by thisBeanOverrideHandler.
-
Constructor Details
-
BeanOverrideHandler
@Deprecated(since="6.2.6", forRemoval=true) protected BeanOverrideHandler(@Nullable Field field, ResolvableType beanType, @Nullable String beanName, BeanOverrideStrategy strategy) Deprecated, for removal: This API element is subject to removal in a future version.As of Spring Framework 6.2.6, in favor ofBeanOverrideHandler(Field, ResolvableType, String, String, BeanOverrideStrategy)Construct a newBeanOverrideHandlerfrom the supplied values.To provide proper support for
@ContextHierarchy, invokeBeanOverrideHandler(Field, ResolvableType, String, String, BeanOverrideStrategy)instead.- Parameters:
field- theFieldannotated with@BeanOverride, ornullif@BeanOverridewas declared at the type levelbeanType- the type of bean to overridebeanName- the name of the bean to override, ornullto look for a single matching bean by typestrategy- theBeanOverrideStrategyto use
-
BeanOverrideHandler
protected BeanOverrideHandler(@Nullable Field field, ResolvableType beanType, @Nullable String beanName, String contextName, BeanOverrideStrategy strategy) Construct a newBeanOverrideHandlerfrom the supplied values.- Parameters:
field- theFieldannotated with@BeanOverride, ornullif@BeanOverridewas declared at the type levelbeanType- the type of bean to overridebeanName- the name of the bean to override, ornullto look for a single matching bean by typecontextName- the name of the context hierarchy level in which the handler should be applied, or an empty string to indicate that the handler should be applied to all application contexts within a context hierarchystrategy- theBeanOverrideStrategyto use- Since:
- 6.2.6
-
-
Method Details
-
forTestClass
Process the giventestClassand build the correspondingBeanOverrideHandlerlist derived from@BeanOverridefields in the test class and its type hierarchy.This method does not search the enclosing class hierarchy and does not search for
@BeanOverridedeclarations on classes or interfaces.- Parameters:
testClass- the test class to process- Returns:
- a list of bean override handlers
- See Also:
-
getField
Get theFieldannotated with@BeanOverride. -
getBeanType
Get the bean type to override. -
getBeanName
Get the bean name to override, ornullto look for a single matching bean of typegetBeanType(). -
getContextName
Get the name of the context hierarchy level in which this handler should be applied.An empty string indicates that this handler should be applied to all application contexts.
If a context name is configured for this handler, it must match a name configured via
@ContextConfiguration(name=...).- Since:
- 6.2.6
- See Also:
-
getStrategy
Get theBeanOverrideStrategyfor thisBeanOverrideHandler, which influences how and when the bean override instance should be created. -
createOverrideInstance
protected abstract Object createOverrideInstance(String beanName, @Nullable BeanDefinition existingBeanDefinition, @Nullable Object existingBeanInstance) Create a bean override instance for an existingBeanDefinitionor an existing singleton bean, based on the metadata in thisBeanOverrideHandler.- Parameters:
beanName- the name of the bean being overriddenexistingBeanDefinition- an existing bean definition for the supplied bean name, ornullif not available or not relevantexistingBeanInstance- an existing instance for the supplied bean name for wrapping purposes, ornullif not available or not relevant- Returns:
- the instance with which to override the bean
- See Also:
-
trackOverrideInstance
Track the supplied bean override instance that was created by thisBeanOverrideHandler.The default implementation does not track the supplied instance, but this can be overridden in subclasses as appropriate.
- Parameters:
override- the bean override instance to tracksingletonBeanRegistry- a registry in which this handler can store tracking state in the form of a singleton bean- See Also:
-
equals
-
hashCode
-
toString
-
BeanOverrideHandler(Field, ResolvableType, String, String, BeanOverrideStrategy)