Annotation Interface ResourcePropertySource


A variant of the @PropertySource annotation that has some limitations:
  • The @PropertySource annotation can't meta-annotate the another annotation
  • 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

This annotation provides additional features not available with the standard PropertySource, such as:

  • Support for meta-annotation usage
  • Control over the order in which property sources are added to Spring's Environment
  • Inheritance through the use of the Inherited annotation
  • Support for resource location wildcards in the resource paths
  • Ability to specify default encoding for property resources

Examples Usage

Basic Usage

To load a simple properties file from the classpath:

 @ResourcePropertySource("classpath:/com/example/config/app.properties")
 public class AppConfig {
 }
 

Specifying Order

To control the precedence of this property source, you can use the attributes like before() or first():

 // Make this property source first in the list
 @ResourcePropertySource(value = "classpath:/com/example/config/app.properties", first = true)
 public class AppConfig {
 }
 

Using Wildcards and Custom Comparator

When using wildcards to load multiple property files, you can also define the loading order via a custom comparator:

 @ResourcePropertySource(
     value = "classpath:/com/example/config/*.properties",
     resourceComparator = MyCustomResourceComparator.class
 )
 public class AppConfig {
 }
 

Ignoring Missing Resources

If the property file is optional, you can choose to ignore missing files:

 @ResourcePropertySource(
     value = "classpath:/com/example/config/app.properties",
     ignoreResourceNotFound = true
 )
 public class AppConfig {
 }
 

Encoding Support

You can also specify the encoding used when reading property files:

 @ResourcePropertySource(
     value = "classpath:/com/example/config/app.properties",
     encoding = "ISO-8859-1"
 )
 public class AppConfig {
 }
 

Custom PropertySourceFactory

To customize how the property source is created, provide your own implementation of PropertySourceFactory:

 @ResourcePropertySource(
     value = "classpath:/com/example/config/app.properties",
     factory = MyCustomPropertySourceFactory.class
 )
 public class AppConfig {
 }
 
Since:
1.0.0
Author:
Mercy
See Also:
  • PropertySource
  • PropertySource
  • Configuration
  • 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 location(s) of the property source file to be loaded.
  • Element Details

    • name

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

      @AliasFor(annotation=PropertySourceExtension.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=PropertySourceExtension.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=PropertySourceExtension.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=PropertySourceExtension.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=PropertySourceExtension.class) String[] value
      Indicate the resource location(s) of the property source file to be loaded.

      Both traditional and XML-based properties file formats are supported — for example, "classpath:/com/myco/app.properties" or "file:/path/to/file.xml".

      Resource location wildcards (e.g. **/*.properties) 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=PropertySourceExtension.class) 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

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

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

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