Commit c86284ce authored by Eddú Meléndez's avatar Eddú Meléndez Committed by Stephane Nicoll

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
parent 54bea725
...@@ -32,10 +32,12 @@ import org.springframework.core.env.Environment; ...@@ -32,10 +32,12 @@ import org.springframework.core.env.Environment;
* properties to be sourced from the Spring environment. * properties to be sourced from the Spring environment.
* *
* @author Phillip Webb * @author Phillip Webb
* @author Eddú Meléndez
*/ */
class SpringPropertyAction extends Action { class SpringPropertyAction extends Action {
private static final String SOURCE_ATTRIBUTE = "source"; private static final String SOURCE_ATTRIBUTE = "source";
private static final String DEFAULT_VALUE_ATTRIBUTE = "defaultValue";
private final Environment environment; private final Environment environment;
...@@ -49,18 +51,22 @@ class SpringPropertyAction extends Action { ...@@ -49,18 +51,22 @@ class SpringPropertyAction extends Action {
String name = attributes.getValue(NAME_ATTRIBUTE); String name = attributes.getValue(NAME_ATTRIBUTE);
String source = attributes.getValue(SOURCE_ATTRIBUTE); String source = attributes.getValue(SOURCE_ATTRIBUTE);
Scope scope = ActionUtil.stringToScope(attributes.getValue(SCOPE_ATTRIBUTE)); Scope scope = ActionUtil.stringToScope(attributes.getValue(SCOPE_ATTRIBUTE));
String defaultValue = attributes.getValue(DEFAULT_VALUE_ATTRIBUTE);
if (OptionHelper.isEmpty(name) || OptionHelper.isEmpty(source)) { if (OptionHelper.isEmpty(name) || OptionHelper.isEmpty(source)) {
addError( addError(
"The \"name\" and \"source\" attributes of <springProperty> must be set"); "The \"name\" and \"source\" attributes of <springProperty> 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) { if (this.environment == null) {
addWarn("No Spring Environment available to resolve " + source); addWarn("No Spring Environment available to resolve " + source);
return null; return null;
} }
if (source == null) {
return defaultValue;
}
String value = this.environment.getProperty(source); String value = this.environment.getProperty(source);
if (value != null) { if (value != null) {
return value; return value;
......
...@@ -140,6 +140,12 @@ public class SpringBootJoranConfiguratorTests { ...@@ -140,6 +140,12 @@ public class SpringBootJoranConfiguratorTests {
assertThat(this.context.getProperty("MINE")).isEqualTo("test"); 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) private void doTestNestedProfile(boolean expected, String... profiles)
throws JoranException { throws JoranException {
this.environment.setActiveProfiles(profiles); this.environment.setActiveProfiles(profiles);
......
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml" />
<springProperty scope="context" name="MINE" defaultValue="foo"/>
</configuration>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment