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.

Specific implementations of metadata can have state to be used during override instance creation — for example, based on further parsing of the annotation or the annotated field.

Since:
6.2
Author:
Simon Baslé, Stephane Nicoll
  • Constructor Details

  • Method Details

    • forTestClass

      public static List<OverrideMetadata> forTestClass(Class<?> testClass)
      Parse the given testClass and build the corresponding list of bean OverrideMetadata.
      Parameters:
      testClass - the class to parse
      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 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 instance, as a hint on how and when the override instance should be created.
    • createOverride

      protected abstract Object createOverride(String beanName, @Nullable BeanDefinition existingBeanDefinition, @Nullable Object existingBeanInstance)
      Create an override instance from this OverrideMetadata, optionally provided with 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 not relevant
      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 is not to track, but this can be overridden in subclasses.

      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