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.
 
NOTE: Only singleton beans can be overridden. Any attempt to override a non-singleton bean will result in an exception.
- Since:
- 6.2
- Author:
- Simon Baslé, Stephane Nicoll, Sam Brannen
- 
Constructor SummaryConstructorsModifierConstructorDescriptionprotectedBeanOverrideHandler(Field field, ResolvableType beanType, String beanName, String contextName, BeanOverrideStrategy strategy) Construct a newBeanOverrideHandlerfrom the supplied values.protectedBeanOverrideHandler(Field field, ResolvableType beanType, String beanName, BeanOverrideStrategy strategy) Deprecated, for removal: This API element is subject to removal in a future version.
- 
Method SummaryModifier and TypeMethodDescriptionprotected abstract ObjectcreateOverrideInstance(String beanName, BeanDefinition existingBeanDefinition, 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.final StringGet 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.final FieldgetField()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- the- Fieldannotated with- @BeanOverride, or- nullif- @BeanOverridewas declared at the type level
- beanType- the type of bean to override
- beanName- the name of the bean to override, or- nullto look for a single matching bean by type
- strategy- the- BeanOverrideStrategyto use
 
- 
BeanOverrideHandlerprotected BeanOverrideHandler(@Nullable Field field, ResolvableType beanType, @Nullable String beanName, String contextName, BeanOverrideStrategy strategy) Construct a newBeanOverrideHandlerfrom the supplied values.- Parameters:
- field- the- Fieldannotated with- @BeanOverride, or- nullif- @BeanOverridewas declared at the type level
- beanType- the type of bean to override
- beanName- the name of the bean to override, or- nullto look for a single matching bean by type
- contextName- 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 hierarchy
- strategy- the- BeanOverrideStrategyto use
- Since:
- 6.2.6
 
 
- 
- 
Method Details- 
forTestClassProcess 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:
- 
- findAllHandlers(Class)
 
 
- 
getFieldGet theFieldannotated with@BeanOverride.
- 
getBeanTypeGet the bean type to override.
- 
getBeanNameGet the bean name to override, ornullto look for a single matching bean of typegetBeanType().
- 
getContextNameGet 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:
 
- 
getStrategyGet theBeanOverrideStrategyfor thisBeanOverrideHandler, which influences how and when the bean override instance should be created.
- 
createOverrideInstanceprotected 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 overridden
- existingBeanDefinition- an existing bean definition for the supplied bean name, or- nullif not available or not relevant
- existingBeanInstance- an existing instance for the supplied bean name for wrapping purposes, or- nullif not available or not relevant
- Returns:
- the instance with which to override the bean
- See Also:
 
- 
trackOverrideInstanceTrack 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 track
- singletonBeanRegistry- a registry in which this handler can store tracking state in the form of a singleton bean
- See Also:
 
- 
equals
- 
hashCodepublic int hashCode()
- 
toString
 
- 
BeanOverrideHandler(Field, ResolvableType, String, String, BeanOverrideStrategy)