Class OverrideMetadata

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

public abstract class OverrideMetadata extends Object
Metadata for Bean Override injection points, also responsible for creation of the overriding instance.

WARNING: implementations are used as a cache key and must implement proper equals() and hashCode() methods.

Concrete implementations of OverrideMetadata can store state 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<OverrideMetadata> forTestClass(Class<?> testClass)
      Process the given testClass and build the corresponding list of OverrideMetadata derived from @BeanOverride fields in the test class and its type hierarchy.
      Parameters:
      testClass - the test class to process
      Returns:
      a list of OverrideMetadata
    • 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 OverrideMetadata, which influences how and when the bean override instance should be created.
    • createOverride

      protected abstract Object createOverride(String beanName, @Nullable BeanDefinition existingBeanDefinition, @Nullable Object existingBeanInstance)
      Create a bean override instance for this OverrideMetadata, optionally based on an existing BeanDefinition and/or an original instance, that is a singleton or an early wrapped instance.
      Parameters:
      beanName - the name of the bean being overridden
      existingBeanDefinition - an existing bean definition for the supplied bean name, or null if irrelevant
      existingBeanInstance - an existing instance for the supplied bean name for wrapping purposes, or null if irrelevant
      Returns:
      the instance with which to override the bean
    • track

      protected void track(Object override, SingletonBeanRegistry trackingBeanRegistry)
      Optionally track objects created by this OverrideMetadata.

      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
      trackingBeanRegistry - the registry in which trackers can optionally be registered
    • 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