Class AnnotatedPropertySourceLoader<A extends Annotation>

java.lang.Object
io.microsphere.spring.context.annotation.BeanCapableImportCandidate
io.microsphere.spring.config.context.annotation.AnnotatedPropertySourceLoader<A>
Type Parameters:
A - The type of annotation that triggers property source loading
All Implemented Interfaces:
org.springframework.beans.factory.Aware, org.springframework.beans.factory.BeanClassLoaderAware, org.springframework.beans.factory.BeanFactoryAware, org.springframework.context.annotation.ImportSelector, org.springframework.context.EnvironmentAware, org.springframework.context.ResourceLoaderAware
Direct Known Subclasses:
PropertySourceExtensionLoader

public abstract class AnnotatedPropertySourceLoader<A extends Annotation> extends BeanCapableImportCandidate implements org.springframework.context.annotation.ImportSelector
Abstract base class for ImportSelector implementations that load a PropertySource when a Configuration class is annotated with a specific annotation.

This class provides a foundation for conditionally adding property sources to the Spring environment based on annotations present on configuration classes. Subclasses must implement the loadPropertySource(AnnotationAttributes, AnnotationMetadata, String, MutablePropertySources) method to define how the property source is loaded.

Example Usage


 public class MyPropertySourceLoader extends AnnotatedPropertySourceLoader<MyPropertySource> {

     @Override
     protected void loadPropertySource(AnnotationAttributes attributes,
                                       AnnotationMetadata metadata,
                                       String propertySourceName,
                                       MutablePropertySources propertySources) throws Throwable {
         // Create and add a custom PropertySource
         PropertySource<?> propertySource = new CustomPropertySource(propertySourceName);
         propertySources.addLast(propertySource);
     }
 }
 
Since:
1.0.0
Author:
Mercy
See Also:
  • Field Details

    • NAME_ATTRIBUTE_NAME

      protected static final String NAME_ATTRIBUTE_NAME
      See Also:
    • logger

      protected final io.microsphere.logging.Logger logger
  • Constructor Details

    • AnnotatedPropertySourceLoader

      public AnnotatedPropertySourceLoader()
  • Method Details

    • resolveAnnotationType

      protected Class<A> resolveAnnotationType()
    • selectImports

      public final String[] selectImports(org.springframework.core.type.AnnotationMetadata metadata)
      Specified by:
      selectImports in interface org.springframework.context.annotation.ImportSelector
    • resolvePropertySourceName

      @Nonnull protected final String resolvePropertySourceName(org.springframework.core.annotation.AnnotationAttributes attributes, org.springframework.core.type.AnnotationMetadata metadata)
      Resolve the name of PropertySource
      Parameters:
      attributes - AnnotationAttributes
      metadata - AnnotationMetadata
      Returns:
      non-null
    • buildPropertySourceName

      @Nullable protected String buildPropertySourceName(org.springframework.core.annotation.AnnotationAttributes attributes, org.springframework.core.type.AnnotationMetadata metadata)
      Build the name of PropertySource
      Parameters:
      attributes - AnnotationAttributes
      metadata - AnnotationMetadata
      Returns:
      the attribute value of annotation if the "name" attribute present, or null
    • buildDefaultPropertySourceName

      @Nonnull protected String buildDefaultPropertySourceName(org.springframework.core.annotation.AnnotationAttributes attributes, org.springframework.core.type.AnnotationMetadata metadata)
      Build the default name of PropertySource
      Parameters:
      attributes - AnnotationAttributes
      metadata - AnnotationMetadata
      Returns:
      non-null
    • loadPropertySource

      @Nullable protected abstract void loadPropertySource(org.springframework.core.annotation.AnnotationAttributes attributes, org.springframework.core.type.AnnotationMetadata metadata, String propertySourceName, org.springframework.core.env.MutablePropertySources propertySources) throws Throwable
      Load the PropertySource
      Parameters:
      attributes - AnnotationAttributes
      metadata - AnnotationMetadata
      propertySourceName - the name of PropertySource
      propertySources - MutablePropertySources to be added
      Throws:
      Throwable - the failure of the loading
    • getAnnotationType

      @Nonnull public final Class<A> getAnnotationType()
      The annotation type
      Returns:
      non-null
    • getPropertySourceName

      protected String getPropertySourceName()