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 55523ff5c4c..423c3d538e7 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 46c49c57d71..007fb8271f6 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 00000000000..2d309b94758 --- /dev/null +++ b/spring-boot/src/test/resources/org/springframework/boot/logging/logback/property-defaultValue.xml @@ -0,0 +1,5 @@ + + + + +