Browse Source

Correct code example for YamlProcessor.setDocumentMatchers

Issue: SPR-16849
pull/1853/merge
Juergen Hoeller 7 years ago
parent
commit
7ece0e219e
  1. 14
      spring-beans/src/main/java/org/springframework/beans/factory/config/YamlProcessor.java
  2. 6
      spring-beans/src/test/java/org/springframework/beans/factory/config/YamlPropertiesFactoryBeanTests.java

14
spring-beans/src/main/java/org/springframework/beans/factory/config/YamlProcessor.java

@ -75,15 +75,16 @@ public abstract class YamlProcessor { @@ -75,15 +75,16 @@ public abstract class YamlProcessor {
* name: My Cool App
* </pre>
* when mapped with
* <code>documentMatchers = YamlProcessor.mapMatcher({"environment": "prod"})</code>
* <pre class="code">
* setDocumentMatchers(properties ->
* ("prod".equals(properties.getProperty("environment")) ? MatchStatus.FOUND : MatchStatus.NOT_FOUND));
* </pre>
* would end up as
* <pre class="code">
* environment=prod
* url=http://foo.bar.com
* name=My Cool App
* url=http://dev.bar.com
* </pre>
* @param matchers a map of keys to value patterns (regular expressions)
*/
public void setDocumentMatchers(DocumentMatcher... matchers) {
this.documentMatchers = Arrays.asList(matchers);
@ -92,8 +93,7 @@ public abstract class YamlProcessor { @@ -92,8 +93,7 @@ public abstract class YamlProcessor {
/**
* Flag indicating that a document for which all the
* {@link #setDocumentMatchers(DocumentMatcher...) document matchers} abstain will
* nevertheless match.
* @param matchDefault the flag to set (default true)
* nevertheless match. Default is {@code true}.
*/
public void setMatchDefault(boolean matchDefault) {
this.matchDefault = matchDefault;
@ -102,9 +102,7 @@ public abstract class YamlProcessor { @@ -102,9 +102,7 @@ public abstract class YamlProcessor {
/**
* 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.
* @param resolutionMethod the resolution method to set (defaults to
* {@link ResolutionMethod#OVERRIDE}).
* from this factory. Default is {@link ResolutionMethod#OVERRIDE}.
*/
public void setResolutionMethod(ResolutionMethod resolutionMethod) {
Assert.notNull(resolutionMethod, "ResolutionMethod must not be null");

6
spring-beans/src/test/java/org/springframework/beans/factory/config/YamlPropertiesFactoryBeanTests.java

@ -107,7 +107,7 @@ public class YamlPropertiesFactoryBeanTests { @@ -107,7 +107,7 @@ public class YamlPropertiesFactoryBeanTests {
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
factory.setResources(new ByteArrayResource(
"foo: bar\nspam: baz\n---\nfoo: bag\nspam: bad".getBytes()));
factory.setDocumentMatchers((DocumentMatcher) properties -> ("bag".equals(properties.getProperty("foo")) ?
factory.setDocumentMatchers(properties -> ("bag".equals(properties.getProperty("foo")) ?
MatchStatus.FOUND : MatchStatus.NOT_FOUND));
Properties properties = factory.getObject();
assertThat(properties.getProperty("foo"), equalTo("bag"));
@ -120,7 +120,7 @@ public class YamlPropertiesFactoryBeanTests { @@ -120,7 +120,7 @@ public class YamlPropertiesFactoryBeanTests {
factory.setMatchDefault(true);
factory.setResources(new ByteArrayResource(
"one: two\n---\nfoo: bar\nspam: baz\n---\nfoo: bag\nspam: bad".getBytes()));
factory.setDocumentMatchers((DocumentMatcher) properties -> {
factory.setDocumentMatchers(properties -> {
if (!properties.containsKey("foo")) {
return MatchStatus.ABSTAIN;
}
@ -161,7 +161,7 @@ public class YamlPropertiesFactoryBeanTests { @@ -161,7 +161,7 @@ public class YamlPropertiesFactoryBeanTests {
factory.setMatchDefault(true);
factory.setResources(new ByteArrayResource(
"one: two\n---\nfoo: bag\nspam: bad\n---\nfoo: bar\nspam: baz".getBytes()));
factory.setDocumentMatchers((DocumentMatcher) properties -> {
factory.setDocumentMatchers(properties -> {
if (!properties.containsKey("foo")) {
return MatchStatus.ABSTAIN;
}

Loading…
Cancel
Save