Class YamlProcessor
- java.lang.Object
- org.springframework.beans.factory.config.YamlProcessor
- Direct Known Subclasses:
YamlMapFactoryBean
,YamlPropertiesFactoryBean
public abstract class YamlProcessor extends Object
Base class for YAML factories.Requires SnakeYAML 1.18 or higher, as of Spring Framework 5.0.6.
- Since:
- 4.1
- Author:
- Dave Syer, Juergen Hoeller, Sam Brannen
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
YamlProcessor.DocumentMatcher
Strategy interface used to test if properties match.static interface
YamlProcessor.MatchCallback
Callback interface used to process the YAML parsing results.static class
YamlProcessor.MatchStatus
Status returned fromYamlProcessor.DocumentMatcher.matches(java.util.Properties)
.static class
YamlProcessor.ResolutionMethod
Method to use for resolving resources.
Constructor Summary
Constructors Constructor Description YamlProcessor()
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected org.yaml.snakeyaml.Yaml
createYaml()
Create theYaml
instance to use.protected Map<String,Object>
getFlattenedMap(Map<String,Object> source)
Return a flattened version of the given map, recursively following any nested Map or Collection values.protected void
process(YamlProcessor.MatchCallback callback)
Provide an opportunity for subclasses to process the Yaml parsed from the supplied resources.void
setDocumentMatchers(YamlProcessor.DocumentMatcher... matchers)
A map of document matchers allowing callers to selectively use only some of the documents in a YAML resource.void
setMatchDefault(boolean matchDefault)
Flag indicating that a document for which all thedocument matchers
abstain will nevertheless match.void
setResolutionMethod(YamlProcessor.ResolutionMethod resolutionMethod)
Method to use for resolving resources.void
setResources(Resource... resources)
Set locations of YAMLresources
to be loaded.void
setSupportedTypes(Class<?>... supportedTypes)
Set the supported types that can be loaded from YAML documents.
Constructor Detail
YamlProcessor
public YamlProcessor()
Method Detail
setDocumentMatchers
public void setDocumentMatchers(YamlProcessor.DocumentMatcher... matchers)
A map of document matchers allowing callers to selectively use only some of the documents in a YAML resource. In YAML documents are separated by---
lines, and each document is converted to properties before the match is made. E.g.environment: dev url: https://dev.bar.com name: Developer Setup --- environment: prod url:https://foo.bar.com name: My Cool App
when mapped withsetDocumentMatchers(properties -> ("prod".equals(properties.getProperty("environment")) ? MatchStatus.FOUND : MatchStatus.NOT_FOUND));
would end up asenvironment=prod url=https://foo.bar.com name=My Cool App
setMatchDefault
public void setMatchDefault(boolean matchDefault)
Flag indicating that a document for which all thedocument matchers
abstain will nevertheless match. Default istrue
.
setResolutionMethod
public void setResolutionMethod(YamlProcessor.ResolutionMethod resolutionMethod)
Method to use for resolving resources. Each resource will be converted to a Map, so this property is used to decide which map entries to keep in the final output from this factory. Default isYamlProcessor.ResolutionMethod.OVERRIDE
.
setResources
public void setResources(Resource... resources)
Set locations of YAMLresources
to be loaded.- See Also:
YamlProcessor.ResolutionMethod
setSupportedTypes
public void setSupportedTypes(Class<?>... supportedTypes)
Set the supported types that can be loaded from YAML documents.If no supported types are configured, all types encountered in YAML documents will be supported. If an unsupported type is encountered, an
IllegalStateException
will be thrown when the corresponding YAML node is processed.- Parameters:
supportedTypes
- the supported types, or an empty array to clear the supported types- Since:
- 5.1.16
- See Also:
createYaml()
process
protected void process(YamlProcessor.MatchCallback callback)
Provide an opportunity for subclasses to process the Yaml parsed from the supplied resources. Each resource is parsed in turn and the documents inside checked against thematchers
. If a document matches it is passed into the callback, along with its representation as Properties. Depending on thesetResolutionMethod(ResolutionMethod)
not all of the documents will be parsed.- Parameters:
callback
- a callback to delegate to once matching documents are found- See Also:
createYaml()
createYaml
protected org.yaml.snakeyaml.Yaml createYaml()
Create theYaml
instance to use.The default implementation sets the "allowDuplicateKeys" flag to
false
, enabling built-in duplicate key handling in SnakeYAML 1.18+.As of Spring Framework 5.1.16, if custom supported types have been configured, the default implementation creates a
Yaml
instance that filters out unsupported types encountered in YAML documents. If an unsupported type is encountered, anIllegalStateException
will be thrown when the node is processed.- See Also:
LoaderOptions.setAllowDuplicateKeys(boolean)
getFlattenedMap
protected final Map<String,Object> getFlattenedMap(Map<String,Object> source)
Return a flattened version of the given map, recursively following any nested Map or Collection values. Entries from the resulting map retain the same order as the source. When called with the Map from aYamlProcessor.MatchCallback
the result will contain the same values as theYamlProcessor.MatchCallback
Properties.- Parameters:
source
- the source map- Returns:
- a flattened map
- Since:
- 4.1.3