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
or the annotated field.
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 Summary
ModifierConstructorDescriptionprotected
BeanOverrideHandler
(Field field, ResolvableType beanType, String beanName, BeanOverrideStrategy strategy) -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract Object
createOverrideInstance
(String beanName, BeanDefinition existingBeanDefinition, Object existingBeanInstance) Create a bean override instance for an existingBeanDefinition
or an existing singleton bean, based on the metadata in thisBeanOverrideHandler
.boolean
static List<BeanOverrideHandler>
forTestClass
(Class<?> testClass) Process the giventestClass
and build the correspondingBeanOverrideHandler
list derived from@BeanOverride
fields in the test class and its type hierarchy.final String
Get the bean name to override, ornull
to look for a single matching bean of typegetBeanType()
.final ResolvableType
Get the bean type to override.final Field
getField()
Get the annotatedField
.final BeanOverrideStrategy
Get theBeanOverrideStrategy
for thisBeanOverrideHandler
, which influences how and when the bean override instance should be created.int
hashCode()
toString()
protected void
trackOverrideInstance
(Object override, SingletonBeanRegistry singletonBeanRegistry) Track the supplied bean override instance that was created by thisBeanOverrideHandler
.
-
Constructor Details
-
BeanOverrideHandler
protected BeanOverrideHandler(Field field, ResolvableType beanType, @Nullable String beanName, BeanOverrideStrategy strategy)
-
-
Method Details
-
forTestClass
Process the giventestClass
and build the correspondingBeanOverrideHandler
list derived from@BeanOverride
fields in the test class and its type hierarchy.This method does not search the enclosing class hierarchy.
- Parameters:
testClass
- the test class to process- Returns:
- a list of bean override handlers
- See Also:
-
getField
Get the annotatedField
. -
getBeanType
Get the bean type to override. -
getBeanName
Get the bean name to override, ornull
to look for a single matching bean of typegetBeanType()
. -
getStrategy
Get theBeanOverrideStrategy
for 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 existingBeanDefinition
or 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, ornull
if not available or not relevantexistingBeanInstance
- an existing instance for the supplied bean name for wrapping purposes, ornull
if 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
public int hashCode() -
toString
-