Annotation Interface YamlPropertySource


The extension annotation of @ResourcePropertySource providing a convenient and declarative mechanism for adding a YAML ImmutableMapPropertySource to Spring's Environment. To be used in conjunction with @Configuration classes.

Example Usage

Example 1: Basic usage

 
 @YamlPropertySource("classpath:/config/app.yaml")
 @Configuration
 public class AppConfig {
 }
 
 

Example 2: Using wildcard and custom sorting

 
 @YamlPropertySource(value = "classpath:/config/*.yaml",
                     resourceComparator = CustomResourceComparator.class)
 @Configuration
 public class AppConfig {
 }
 
 

Example 3: Optional JSON resource

 
 @YamlPropertySource(value = "classpath:/config/optional.yaml", ignoreResourceNotFound = true)
 @Configuration
 public class AppConfig {
 }
 
 

Example 4: Auto-refreshing property source

 
 @YamlPropertySource(value = "file:/data/config/app.yaml", autoRefreshed = true)
 @Configuration
 public class AppConfig {
 }
 
 

Example 5: For specifying the order of property sources


 @YamlPropertySource(value = "classpath:/app.yaml", first = true)
 @Configuration
 public class AppConfig {
     // configuration beans
 }
 

Example 6: Customizing the character encoding


 @YamlPropertySource(value = "classpath:/app.yaml", encoding = "ISO-8859-1")
 @Configuration
 public class AppConfig {
     // configuration beans
 }
 
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.
    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
    Indicate the resource location(s) of the YAML file to be loaded.
  • Element Details

    • name

      @AliasFor(annotation=ResourcePropertySource.class) String name
      Indicate the name of this property source.
      See Also:
      • PropertySource.getName()
      • Resource.getDescription()
      Default:
      ""
    • autoRefreshed

      @AliasFor(annotation=ResourcePropertySource.class) boolean autoRefreshed
      It indicates the property source is auto-refreshed when the configuration is changed.
      Returns:
      default value is false
      Default:
      false
    • first

      @AliasFor(annotation=ResourcePropertySource.class) 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

      @AliasFor(annotation=ResourcePropertySource.class) 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

      @AliasFor(annotation=ResourcePropertySource.class) 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

      @AliasFor(annotation=ResourcePropertySource.class) String[] value
      Indicate the resource location(s) of the YAML file to be loaded.

      For example, "classpath:/com/myco/app.yaml" or "classpath:/com/myco/app.yml" or "file:/path/to/file.yaml".

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

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

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

      Default:
      {}
    • resourceComparator

      @AliasFor(annotation=ResourcePropertySource.class) Class<? extends Comparator<org.springframework.core.io.Resource>> resourceComparator
      Indicate the resources to be sorted when value() specifies the resource location wildcards

      For example, "classpath:/com/myco/*.yaml", suppose there are two resources named "a.yaml" and "b.yaml" 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

      @AliasFor(annotation=ResourcePropertySource.class) boolean ignoreResourceNotFound
      Indicate if a failure to find a property resource should be ignored.

      true is appropriate if the YAML file is completely optional.

      Default is false.

      Default:
      false
    • encoding

      @AliasFor(annotation=ResourcePropertySource.class) String encoding
      A specific character encoding for the given resources.

      Default is "UTF-8"

      Default:
      "UTF-8"