Class BeanOverrideHandler

java.lang.Object
org.springframework.test.context.bean.override.BeanOverrideHandler

public abstract class BeanOverrideHandler extends Object
Handler for Bean Override injection points that is responsible for creating the bean override instance for a given set of metadata and potentially for tracking the created instance.

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.

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 Details

  • Method Details

    • forTestClass

      public static List<BeanOverrideHandler> forTestClass(Class<?> testClass)
      Process the given testClass and build the corresponding BeanOverrideHandler list derived from @BeanOverride fields in the test class, its type hierarchy, and its enclosing class hierarchy.
      Parameters:
      testClass - the test class to process
      Returns:
      a list of bean override handlers
    • getField

      public final Field getField()
      Get the annotated Field.
    • getBeanType

      public final ResolvableType getBeanType()
      Get the bean type to override.
    • getBeanName

      @Nullable public final String getBeanName()
      Get the bean name to override, or null to look for a single matching bean of type getBeanType().
    • getStrategy

      public final BeanOverrideStrategy getStrategy()
      Get the BeanOverrideStrategy for this BeanOverrideHandler, 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 existing BeanDefinition or an existing singleton bean, based on the metadata in this BeanOverrideHandler.
      Parameters:
      beanName - the name of the bean being overridden
      existingBeanDefinition - an existing bean definition for the supplied bean name, or null if not available or not relevant
      existingBeanInstance - an existing instance for the supplied bean name for wrapping purposes, or null if not available or not relevant
      Returns:
      the instance with which to override the bean
      See Also:
    • trackOverrideInstance

      protected void trackOverrideInstance(Object override, SingletonBeanRegistry singletonBeanRegistry)
      Track the supplied bean override instance that was created by this BeanOverrideHandler.

      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

      public boolean equals(Object other)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object