Annotation Interface PropertySourceExtension


@Target(ANNOTATION_TYPE) @Retention(RUNTIME) @Inherited @Documented public @interface PropertySourceExtension
Extension meta-annotation for Spring's @PropertySource to overcome its limitations:
  • The @PropertySource annotation can't auto-refresh the property sources
  • The @PropertySource annotation can't control the order of PropertySource
  • The @PropertySource annotation can't be inherited
  • The PropertySource#value() attribute does not support the resource location wildcards
  • The PropertySource#encoding() attribute does not specify the default encoding for the resource

Features:

  • Supports auto-refreshing property sources when configurations change
  • Allows specifying the order of property sources using:
    • first() - Place this property source at the top
    • before() - Place before a specific property source
    • after() - Place after a specific property source
  • Supports inheritance via the @Inherited annotation
  • Resource location wildcards are supported in the value() attribute
  • Provides control over resource loading behavior with:
  • Customizable property source creation via the factory() attribute

Example Usage

Basic Usage


 // Define a custom annotation using PropertySourceExtension
 @Target(ElementType.TYPE)
 @Retention(RetentionPolicy.RUNTIME)
 @PropertySourceExtension(
     value = "classpath:/my-config/*.properties",
     first = true,
     autoRefreshed = true,
     encoding = "UTF-8",
     ignoreResourceNotFound = true,
     resourceComparator = MyCustomResourceComparator.class
 )
 public @interface MyCustomPropertySource {
 }

 // Use the custom annotation on a configuration class
 @MyCustomPropertySource
 @Configuration
 public class MyConfig {
 }
 

Advanced Usage


 @Target(TYPE)
 @Retention(RUNTIME)
 @Inherited
 @Documented
 @PropertySourceExtension
 @Repeatable(ResourcePropertySources.class)
 @Import(ResourcePropertySourceLoader.class)
 public @interface ResourcePropertySource {
     @AliasFor(annotation = PropertySourceExtension.class)
     String name() default "";

     @AliasFor(annotation = PropertySourceExtension.class)
     boolean autoRefreshed() default false;

     @AliasFor(annotation = PropertySourceExtension.class)
     boolean first() default false;

     @AliasFor(annotation = PropertySourceExtension.class)
     String before() default "";

     @AliasFor(annotation = PropertySourceExtension.class)
     String after() default "";

     @AliasFor(annotation = PropertySourceExtension.class)
     String[] value() default {};

     @AliasFor(annotation = PropertySourceExtension.class)
     Class<? extends Comparator<Resource>> resourceComparator() default DefaultResourceComparator.class;

     @AliasFor(annotation = PropertySourceExtension.class)
     boolean ignoreResourceNotFound() default false;

     @AliasFor(annotation = PropertySourceExtension.class)
     String encoding() default "${file.encoding:UTF-8}";

     @AliasFor(annotation = PropertySourceExtension.class)
     Class<? extends PropertySourceFactory> factory() default DefaultPropertySourceFactory.class;
 }
 

This annotation is designed to be used as a meta-annotation for creating custom annotations that extend the functionality of Spring's built-in @PropertySource. It provides more flexibility and control over how properties are loaded into the Spring environment.

Since:
1.0.0
Author:
Mercy
See Also:
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    The relative order after specified PropertySource
    boolean
    It indicates the property source is auto-refreshed when the configuration is changed.
    The relative order before specified PropertySource
    A specific character encoding for the given resources.
    Class<? extends org.springframework.core.io.support.PropertySourceFactory>
    Specify a custom PropertySourceFactory, if any.
    boolean
    Indicates current PropertySource is first order or not If specified , before() and after() will be ignored, or last order.
    boolean
    Indicate if a failure to find a property resource should be ignored.
    Indicate the name of this property source.
    Class<? extends Comparator<org.springframework.core.io.Resource>>
    Indicate the resources to be sorted when value() specifies the resource location wildcards or the same resource names with the different absolute paths.
    Indicate the resource(s) of the property source to be loaded.
  • Element Details

    • name

      String name
      Indicate the name of this property source.
      See Also:
      • PropertySource.getName()
      • Resource.getDescription()
      Default:
      ""
    • autoRefreshed

      boolean autoRefreshed
      It indicates the property source is auto-refreshed when the configuration is changed.
      Returns:
      default value is false
      Default:
      false
    • first

      boolean first
      Indicates current PropertySource is first order or not If specified , before() and after() will be ignored, or last order.
      Returns:
      default value is false
      Default:
      false
    • before

      String before
      The relative order before specified PropertySource

      If not specified , current PropertySource will be added last.

      If first() specified , current attribute will be ignored.

      Returns:
      the name of PropertySource, default value is the empty string
      Default:
      ""
    • after

      String after
      The relative order after specified PropertySource

      If not specified , current PropertySource will be added last.

      If first() specified , current attribute will be ignored.

      Returns:
      the name of PropertySource, default value is the empty string
      Default:
      ""
    • value

      String[] value
      Indicate the resource(s) of the property source to be loaded.

      For example, "classpath:/com/myco/app.properties" or "file:/path/to/file.xml".

      Resource wildcards (e.g. **/*.properties) also are permitted;

      ${...} placeholders will be resolved against any/all property sources already registered with the Environment.

      Each value will be added to the enclosing Environment as its own property source, and in the order declared.

      Default:
      {}
    • resourceComparator

      Class<? extends Comparator<org.springframework.core.io.Resource>> resourceComparator
      Indicate the resources to be sorted when value() specifies the resource location wildcards or the same resource names with the different absolute paths.

      For example, "classpath:/com/myco/*.properties", suppose there are two resources named "a.properties" and "b.properties" where two instances of Resource will be resolved, they are the sources of PropertySource, thus it has to sort them to indicate the order of PropertySource that will be added to the enclosing Environment.

      Default is DefaultResourceComparator

      See Also:
      Default:
      io.microsphere.spring.config.env.support.DefaultResourceComparator.class
    • ignoreResourceNotFound

      boolean ignoreResourceNotFound
      Indicate if a failure to find a property resource should be ignored.

      true is appropriate if the properties file is completely optional.

      Default is false.

      Default:
      false
    • encoding

      String encoding
      A specific character encoding for the given resources.

      Default is the property value of "file.encoding" if present, or "UTF-8"

      Default:
      "${file.encoding:UTF-8}"
    • factory

      Class<? extends org.springframework.core.io.support.PropertySourceFactory> factory
      Specify a custom PropertySourceFactory, if any.

      By default, a default factory for standard resource files will be used.

      Default is DefaultPropertySourceFactory

      See Also:
      • DefaultPropertySourceFactory
      • ResourcePropertySource
      Default:
      org.springframework.core.io.support.DefaultPropertySourceFactory.class