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 Summary
ConstructorsModifierConstructorDescriptionprotectedBeanOverrideHandler(Field field, ResolvableType beanType, String beanName, BeanOverrideStrategy strategy) -
Method Summary
Modifier 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 FieldgetField()Get the annotatedField.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
protected BeanOverrideHandler(@Nullable Field field, ResolvableType beanType, @Nullable String beanName, BeanOverrideStrategy strategy)
-
-
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:
-
findAllHandlers(Class)
-
getField
Get the annotatedField. -
getBeanType
Get the bean type to override. -
getBeanName
Get the bean name to override, ornullto look for a single matching bean of typegetBeanType(). -
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
public int hashCode() -
toString
-