Class ConfigurationPropertyOverrideAnnotationAttributesStrategy

java.lang.Object
io.microsphere.spring.context.annotation.ConfigurationPropertyOverrideAnnotationAttributesStrategy
All Implemented Interfaces:
OverrideAnnotationAttributesStrategy, org.springframework.beans.factory.Aware, org.springframework.context.EnvironmentAware

public class ConfigurationPropertyOverrideAnnotationAttributesStrategy extends Object implements OverrideAnnotationAttributesStrategy, org.springframework.context.EnvironmentAware
A strategy implementation of OverrideAnnotationAttributesStrategy that overrides annotation attributes with values from Spring Environment configuration properties.

This strategy allows externalizing annotation attribute values into configuration properties (e.g., in application.properties or application.yml). It maps annotation attributes to property keys using a configurable prefix.

Property Mapping Logic

  1. Prefix Determination: The property prefix is determined by:
    • Checking for a custom prefix defined by the property key: microsphere.spring.prefix.<AnnotationClassName> (e.g., microsphere.spring.prefix.org.springframework.context.annotation.PropertySource)
    • Falling back to the default prefix: microsphere.spring.@<AnnotationSimpleName>. (e.g., microsphere.spring.@PropertySource.)
  2. Attribute Mapping: Each annotation attribute name is appended to the resolved prefix to form the full property key. For example, for an attribute value, the property key would be <prefix>value.
  3. Type Conversion: If a configuration property exists for an attribute, it is converted to the attribute's type using the Spring ConversionService. If conversion fails or the property is missing, the original annotation attribute value is retained.

Example Usage

1. Default Prefix Behavior

Given an annotation @PropertySource(name = "default", ignoreResourceNotFound = false):

 // config.properties
 [email protected]=my-custom-source
 [email protected]=true

 // Resulting AnnotationAttributes:
 // name = "my-custom-source"
 // ignoreResourceNotFound = true
 

2. Custom Prefix Behavior

You can define a custom prefix for a specific annotation type:

 // config.properties
 # Define a custom prefix for PropertySource annotation
 microsphere.spring.prefix.org.springframework.context.annotation.PropertySource=app.prop.source.

 # Use the custom prefix to override attributes
 app.prop.source.name=overridden-name
 app.prop.source.ignoreResourceNotFound=true

 // Resulting AnnotationAttributes for @PropertySource:
 // name = "overridden-name"
 // ignoreResourceNotFound = true
 
Since:
1.0.0
Author:
Mercy
See Also:
  • Constructor Details

    • ConfigurationPropertyOverrideAnnotationAttributesStrategy

      public ConfigurationPropertyOverrideAnnotationAttributesStrategy()
  • Method Details

    • override

      public org.springframework.core.annotation.AnnotationAttributes override(org.springframework.core.annotation.AnnotationAttributes originalAttributes, Class<? extends Annotation> annotationType, org.springframework.core.type.AnnotationMetadata annotationMetadata)
      Overrides the original annotation attributes with values from configuration properties.

      This method attempts to find configuration properties that match the annotation's attributes. If a matching property is found and can be converted to the attribute's type, it replaces the original value. Otherwise, the original value is retained.

      Specified by:
      override in interface OverrideAnnotationAttributesStrategy
      Parameters:
      originalAttributes - the original annotation attributes to be potentially overridden
      annotationType - the type of the annotation being processed
      annotationMetadata - the metadata of the annotated element
      Returns:
      a new AnnotationAttributes instance containing the overridden values, or the original attributes if no overrides are applicabl
    • getConfigurationProperties

      @Nonnull protected Map<String,Object> getConfigurationProperties(Class<? extends Annotation> annotationType)
      Gets the configuration properties associated with the specified annotation type.

      This method retrieves sub-properties from the environment using the property name prefix derived from the given annotation type. These properties are intended to override the default attributes of the annotation.

      Example Usage

      
       // Assume the environment contains the following properties:
       // microsphere.spring.prefix.PropertySource.name = "myProperties"
       // microsphere.spring.prefix.PropertySource.ignoreResourceNotFound = true
      
       Map<String, Object> properties = getConfigurationProperties(PropertySource.class);
       // properties will contain:
       // { "name": "myProperties", "ignoreResourceNotFound": true }
       
       
      Parameters:
      annotationType - the type of the annotation for which to retrieve configuration properties
      Returns:
      a map of configuration properties keyed by attribute name, or an empty map if none are found
    • getPropertyName

      @Nonnull protected String getPropertyName(Class<? extends Annotation> annotationType, String attributeName)
      Gets the full property name for the given annotation attribute.

      The property name is constructed by concatenating the property name prefix (obtained via getPropertyNamePrefix(Class)) with the attribute name.

      Example Usage

      
        String propertyName = getPropertyName(PropertySource.class, "factory");
        // propertyName == "microsphere.spring.prefix.PropertySource.factory"
       
       
      Parameters:
      annotationType - the type of the annotation
      attributeName - the name of the annotation attribute
      Returns:
      the full property name corresponding to the annotation attribute
    • getPropertyNamePrefix

      @Nonnull protected String getPropertyNamePrefix(Class<? extends Annotation> annotationType)
      Gets the property name prefix for the given annotation type.

      This method determines the prefix by first checking if a specific prefix property is defined in the environment using getPrefixPropertyName(Class). If not found, it falls back to the default prefix generated by getDefaultPropertyNamePrefix(Class). The resulting prefix is then normalized (ensuring it ends with a dot if not empty).

      Example Usage

      Default

      
        String propertyNamePrefix = getPropertyNamePrefix(PropertySource.class);
        // propertyNamePrefix == "microsphere.spring.prefix.PropertySource."
        // the same as invocation on the method getDefaultPropertyNamePrefix(PropertySource.class)
       
       

      Customized Property Name Prefix

      
        // Assume the following environment property exists:
        // microsphere.spring.prefix.org.springframework.context.annotation.PropertySource = "microsphere.property-source."
        String propertyNamePrefix = getPropertyNamePrefix(PropertySource.class);
        // propertyNamePrefix == "microsphere.property-source."
       
       
      Parameters:
      annotationType - the type of the annotation
      Returns:
      the normalized property name prefix
    • getPrefixPropertyName

      @Nonnull public static String getPrefixPropertyName(Class<? extends Annotation> annotationType)
      Gets the property name used to look up the prefix for the given annotation type.

      The property name is constructed by concatenating the PropertyConstants.PREFIX_PROPERTY_NAME_PREFIX with the fully qualified name of the annotation class.

      Example Usage

      
        String prefixPropertyName = getPrefixPropertyName(PropertySource.class);
        // prefixPropertyName == "microsphere.spring.prefix.io.microsphere.spring.context.annotation.PropertySource"
       
       
      Parameters:
      annotationType - the type of the annotation
      Returns:
      the property name for the prefix
    • getDefaultPropertyNamePrefix

      @Nonnull public static String getDefaultPropertyNamePrefix(Class<? extends Annotation> annotationType)
      Gets the default property name prefix for the given annotation type.

      The default prefix is constructed by concatenating the PropertyConstants.MICROSPHERE_SPRING_PROPERTY_NAME_PREFIX with the simple name of the annotation class and a dot character.

      Example Usage

      
        String defaultPropertyNamePrefix = getDefaultPropertyNamePrefix(PropertySource.class);
        // defaultPropertyNamePrefix == "microsphere.spring.@PropertySource."
       
       
      Parameters:
      annotationType - the type of the annotation
      Returns:
      the default property name prefix
    • setEnvironment

      public void setEnvironment(org.springframework.core.env.Environment environment)
      Specified by:
      setEnvironment in interface org.springframework.context.EnvironmentAware