From c86284cea30d558c938a4eef2d888e209e72ffcc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edd=C3=BA=20Mel=C3=A9ndez?= Date: Tue, 29 Mar 2016 21:36:56 +1000 Subject: [PATCH 1/2] Add `defaultValue` property in springProperty tag This commit adds a `defaultValue` attribute to the `springProperty` tag of Logback. That attribute can be used to specify a default value. Closes gh-5447 --- .../boot/logging/logback/SpringPropertyAction.java | 10 ++++++++-- .../logback/SpringBootJoranConfiguratorTests.java | 6 ++++++ .../boot/logging/logback/property-defaultValue.xml | 5 +++++ 3 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 spring-boot/src/test/resources/org/springframework/boot/logging/logback/property-defaultValue.xml diff --git a/spring-boot/src/main/java/org/springframework/boot/logging/logback/SpringPropertyAction.java b/spring-boot/src/main/java/org/springframework/boot/logging/logback/SpringPropertyAction.java index 55523ff5c4..423c3d538e 100644 --- a/spring-boot/src/main/java/org/springframework/boot/logging/logback/SpringPropertyAction.java +++ b/spring-boot/src/main/java/org/springframework/boot/logging/logback/SpringPropertyAction.java @@ -32,10 +32,12 @@ import org.springframework.core.env.Environment; * properties to be sourced from the Spring environment. * * @author Phillip Webb + * @author Eddú Meléndez */ class SpringPropertyAction extends Action { private static final String SOURCE_ATTRIBUTE = "source"; + private static final String DEFAULT_VALUE_ATTRIBUTE = "defaultValue"; private final Environment environment; @@ -49,18 +51,22 @@ class SpringPropertyAction extends Action { String name = attributes.getValue(NAME_ATTRIBUTE); String source = attributes.getValue(SOURCE_ATTRIBUTE); Scope scope = ActionUtil.stringToScope(attributes.getValue(SCOPE_ATTRIBUTE)); + String defaultValue = attributes.getValue(DEFAULT_VALUE_ATTRIBUTE); if (OptionHelper.isEmpty(name) || OptionHelper.isEmpty(source)) { addError( "The \"name\" and \"source\" attributes of must be set"); } - ActionUtil.setProperty(ic, name, getValue(source), scope); + ActionUtil.setProperty(ic, name, getValue(source, defaultValue), scope); } - private String getValue(String source) { + private String getValue(String source, String defaultValue) { if (this.environment == null) { addWarn("No Spring Environment available to resolve " + source); return null; } + if (source == null) { + return defaultValue; + } String value = this.environment.getProperty(source); if (value != null) { return value; diff --git a/spring-boot/src/test/java/org/springframework/boot/logging/logback/SpringBootJoranConfiguratorTests.java b/spring-boot/src/test/java/org/springframework/boot/logging/logback/SpringBootJoranConfiguratorTests.java index 46c49c57d7..007fb8271f 100644 --- a/spring-boot/src/test/java/org/springframework/boot/logging/logback/SpringBootJoranConfiguratorTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/logging/logback/SpringBootJoranConfiguratorTests.java @@ -140,6 +140,12 @@ public class SpringBootJoranConfiguratorTests { assertThat(this.context.getProperty("MINE")).isEqualTo("test"); } + @Test + public void springPropertyWithDefaultValue() throws Exception { + initialize("property-defaultValue.xml"); + assertThat(this.context.getProperty("MINE")).isEqualTo("foo"); + } + private void doTestNestedProfile(boolean expected, String... profiles) throws JoranException { this.environment.setActiveProfiles(profiles); diff --git a/spring-boot/src/test/resources/org/springframework/boot/logging/logback/property-defaultValue.xml b/spring-boot/src/test/resources/org/springframework/boot/logging/logback/property-defaultValue.xml new file mode 100644 index 0000000000..2d309b9475 --- /dev/null +++ b/spring-boot/src/test/resources/org/springframework/boot/logging/logback/property-defaultValue.xml @@ -0,0 +1,5 @@ + + + + + From a66045fa9853fa35f9bd4b28dc68c68035649d2a Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Tue, 29 Mar 2016 15:10:50 +0200 Subject: [PATCH 2/2] Polish contribution Closes gh-5521 --- .../main/asciidoc/spring-boot-features.adoc | 7 ++++-- .../logging/logback/SpringPropertyAction.java | 7 ++---- .../SpringBootJoranConfiguratorTests.java | 23 +++++++++++++++++-- .../logback/property-default-value.xml | 6 +++++ .../logging/logback/property-defaultValue.xml | 5 ---- .../boot/logging/logback/property.xml | 1 + 6 files changed, 35 insertions(+), 14 deletions(-) create mode 100644 spring-boot/src/test/resources/org/springframework/boot/logging/logback/property-default-value.xml delete mode 100644 spring-boot/src/test/resources/org/springframework/boot/logging/logback/property-defaultValue.xml diff --git a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc index 7afcb29101..159f4e7e06 100644 --- a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc +++ b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc @@ -1351,11 +1351,14 @@ for use within Logback. This can be useful if you want to access values from you `application.properties` file in your logback configuration. The tag works in a similar way to Logback's standard `` tag, but rather than specifying a direct `value` you specify the `source` of the property (from the `Environment`). You can use the `scope` -attribute if you need to store the property somewhere other than in `local` scope. +attribute if you need to store the property somewhere other than in `local` scope. If +you need a fallback value in case the property is not set in the `Environment`, you can +use the `defaultValue` attribute. [source,xml,indent=0] ---- - + ${fluentHost} ... diff --git a/spring-boot/src/main/java/org/springframework/boot/logging/logback/SpringPropertyAction.java b/spring-boot/src/main/java/org/springframework/boot/logging/logback/SpringPropertyAction.java index 423c3d538e..f9ad96dafc 100644 --- a/spring-boot/src/main/java/org/springframework/boot/logging/logback/SpringPropertyAction.java +++ b/spring-boot/src/main/java/org/springframework/boot/logging/logback/SpringPropertyAction.java @@ -62,9 +62,6 @@ class SpringPropertyAction extends Action { private String getValue(String source, String defaultValue) { if (this.environment == null) { addWarn("No Spring Environment available to resolve " + source); - return null; - } - if (source == null) { return defaultValue; } String value = this.environment.getProperty(source); @@ -76,9 +73,9 @@ class SpringPropertyAction extends Action { String prefix = source.substring(0, lastDot + 1); RelaxedPropertyResolver resolver = new RelaxedPropertyResolver( this.environment, prefix); - return resolver.getProperty(source.substring(lastDot + 1)); + return resolver.getProperty(source.substring(lastDot + 1), defaultValue); } - return null; + return defaultValue; } @Override diff --git a/spring-boot/src/test/java/org/springframework/boot/logging/logback/SpringBootJoranConfiguratorTests.java b/spring-boot/src/test/java/org/springframework/boot/logging/logback/SpringBootJoranConfiguratorTests.java index 007fb8271f..e1b99132a6 100644 --- a/spring-boot/src/test/java/org/springframework/boot/logging/logback/SpringBootJoranConfiguratorTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/logging/logback/SpringBootJoranConfiguratorTests.java @@ -41,6 +41,7 @@ import static org.hamcrest.Matchers.not; * * @author Phillip Webb * @author Eddú Meléndez + * @author Stephane Nicoll */ public class SpringBootJoranConfiguratorTests { @@ -140,10 +141,28 @@ public class SpringBootJoranConfiguratorTests { assertThat(this.context.getProperty("MINE")).isEqualTo("test"); } + @Test + public void springPropertyNoValue() throws Exception { + initialize("property.xml"); + assertThat(this.context.getProperty("SIMPLE")).isNull(); + } + + @Test + public void relaxedSpringPropertyNoValue() throws Exception { + initialize("property.xml"); + assertThat(this.context.getProperty("MINE")).isNull(); + } + @Test public void springPropertyWithDefaultValue() throws Exception { - initialize("property-defaultValue.xml"); - assertThat(this.context.getProperty("MINE")).isEqualTo("foo"); + initialize("property-default-value.xml"); + assertThat(this.context.getProperty("SIMPLE")).isEqualTo("foo"); + } + + @Test + public void relaxedSpringPropertyWithDefaultValue() throws Exception { + initialize("property-default-value.xml"); + assertThat(this.context.getProperty("MINE")).isEqualTo("bar"); } private void doTestNestedProfile(boolean expected, String... profiles) diff --git a/spring-boot/src/test/resources/org/springframework/boot/logging/logback/property-default-value.xml b/spring-boot/src/test/resources/org/springframework/boot/logging/logback/property-default-value.xml new file mode 100644 index 0000000000..dd00a99b87 --- /dev/null +++ b/spring-boot/src/test/resources/org/springframework/boot/logging/logback/property-default-value.xml @@ -0,0 +1,6 @@ + + + + + + diff --git a/spring-boot/src/test/resources/org/springframework/boot/logging/logback/property-defaultValue.xml b/spring-boot/src/test/resources/org/springframework/boot/logging/logback/property-defaultValue.xml deleted file mode 100644 index 2d309b9475..0000000000 --- a/spring-boot/src/test/resources/org/springframework/boot/logging/logback/property-defaultValue.xml +++ /dev/null @@ -1,5 +0,0 @@ - - - - - diff --git a/spring-boot/src/test/resources/org/springframework/boot/logging/logback/property.xml b/spring-boot/src/test/resources/org/springframework/boot/logging/logback/property.xml index 5b9e062b32..15ef50ae32 100644 --- a/spring-boot/src/test/resources/org/springframework/boot/logging/logback/property.xml +++ b/spring-boot/src/test/resources/org/springframework/boot/logging/logback/property.xml @@ -1,5 +1,6 @@ +