Annotation Interface JsonPropertySource
@ResourcePropertySource
providing a convenient and declarative
mechanism for adding a JSON PropertySource
to Spring's Environment.
To be used in conjunction with @Configuration
classes.
Using JsonPropertySource
allows declarative loading of external JSON configuration files into the Spring
application context's environment. It supports both single and wildcard resource locations, with optional sorting and
encoding configurations.
Example Usage
Example 1: Basic usage
@JsonPropertySource("classpath:/config/app.json")
@Configuration
public class AppConfig {
}
Example 2: Using wildcard and custom sorting
@JsonPropertySource(value = "classpath:/config/*.json",
resourceComparator = CustomResourceComparator.class)
@Configuration
public class AppConfig {
}
Example 3: Optional JSON resource
@JsonPropertySource(value = "classpath:/config/optional.json", ignoreResourceNotFound = true)
@Configuration
public class AppConfig {
}
Example 4: Auto-refreshing property source
@JsonPropertySource(value = "file:/data/config/app.json", autoRefreshed = true)
@Configuration
public class AppConfig {
}
Example 5: For specifying the order of property sources
@JsonPropertySource(value = "classpath:/app.json", first = true)
@Configuration
public class AppConfig {
// configuration beans
}
Example 6: Customizing the character encoding
@JsonPropertySource(value = "classpath:/app.json", encoding = "ISO-8859-1")
@Configuration
public class AppConfig {
// configuration beans
}
- Since:
- 1.0.0
- Author:
- Mercy
- See Also:
-
ResourcePropertySource
JsonPropertySourceFactory
MapPropertySource
-
Optional Element Summary
Optional ElementsModifier and TypeOptional ElementDescriptionThe relative order after specifiedPropertySource
boolean
It indicates the property source is auto-refreshed when the configuration is changed.The relative order before specifiedPropertySource
A specific character encoding for the given resources.boolean
boolean
Indicate if a failure to find aproperty 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 whenvalue()
specifies the resource location wildcardsString[]
Indicate the resource location(s) of the JSON file to be loaded.
-
Element Details
-
name
Indicate the name of this property source.- See Also:
-
PropertySource.getName()
Resource.getDescription()
- Default:
- ""
-
autoRefreshed
It indicates the property source is auto-refreshed when the configuration is changed.- Returns:
- default value is
false
- Default:
- false
-
first
Indicates currentPropertySource
is first order or not If specified ,before()
andafter()
will be ignored, or last order.- Returns:
- default value is
false
- Default:
- false
-
before
The relative order before specifiedPropertySource
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
The relative order after specifiedPropertySource
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
Indicate the resource location(s) of the JSON file to be loaded.For example,
"classpath:/com/myco/app.json"
Resource location wildcards (e.g. **/*.json) 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>> resourceComparatorIndicate the resources to be sorted whenvalue()
specifies the resource location wildcardsFor example,
"classpath:/com/myco/*.json"
, suppose there are two resources named "a.json" and "b.json" where two instances ofResource
will be resolved, they are the sources ofPropertySource
, thus it has to sort them to indicate the order ofPropertySource
that will be added to the enclosingEnvironment
.Default is
DefaultResourceComparator
- See Also:
- Default:
- io.microsphere.spring.config.env.support.DefaultResourceComparator.class
-
ignoreResourceNotFound
Indicate if a failure to find aproperty resource
should be ignored.true
is appropriate if the JSON file is completely optional.Default is
false
.- Default:
- false
-
encoding
A specific character encoding for the given resources.Default is "UTF-8"
- Default:
- "UTF-8"
-