Skip to content

Commit 2d280bb

Browse files
committed
Polish "Fix support for default values in banner placeholders"
See gh-34764
1 parent 5c01aa7 commit 2d280bb

File tree

2 files changed

+13
-26
lines changed

2 files changed

+13
-26
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ResourceBanner.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@
2828
import org.apache.commons.logging.LogFactory;
2929

3030
import org.springframework.boot.ansi.AnsiPropertySource;
31+
import org.springframework.core.env.ConfigurableEnvironment;
3132
import org.springframework.core.env.Environment;
3233
import org.springframework.core.env.MapPropertySource;
3334
import org.springframework.core.env.MutablePropertySources;
3435
import org.springframework.core.env.PropertyResolver;
35-
import org.springframework.core.env.PropertySource;
3636
import org.springframework.core.env.PropertySourcesPropertyResolver;
3737
import org.springframework.core.io.Resource;
3838
import org.springframework.core.log.LogMessage;
@@ -79,22 +79,15 @@ public void printBanner(Environment environment, Class<?> sourceClass, PrintStre
7979

8080
protected List<PropertyResolver> getPropertyResolvers(Environment environment, Class<?> sourceClass) {
8181
MutablePropertySources propertySources = new MutablePropertySources();
82-
propertySources.addLast(getEnvironmentSource(environment));
82+
if (environment instanceof ConfigurableEnvironment) {
83+
((ConfigurableEnvironment) environment).getPropertySources().forEach(propertySources::addLast);
84+
}
8385
propertySources.addLast(getTitleSource(sourceClass));
8486
propertySources.addLast(getAnsiSource());
8587
propertySources.addLast(getVersionSource(sourceClass));
8688
return Collections.singletonList(new PropertySourcesPropertyResolver(propertySources));
8789
}
8890

89-
private PropertySource<Environment> getEnvironmentSource(Environment environment) {
90-
return new PropertySource<Environment>("environment", environment) {
91-
@Override
92-
public Object getProperty(String name) {
93-
return environment.getProperty(name);
94-
}
95-
};
96-
}
97-
9891
private MapPropertySource getVersionSource(Class<?> sourceClass) {
9992
return new MapPropertySource("version", getVersionsMap(sourceClass));
10093
}

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/ResourceBannerTests.java

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -49,21 +49,6 @@ void reset() {
4949
AnsiOutput.setEnabled(Enabled.DETECT);
5050
}
5151

52-
@Test
53-
void doNotUseDefaultsIfValueExists() {
54-
Resource resource = new ByteArrayResource(
55-
"banner ${a:def} ${spring-boot.version:def} ${application.version:def}".getBytes());
56-
String banner = printBanner(resource, "10.2", "1.0", null);
57-
assertThat(banner).startsWith("banner 1 10.2 1.0");
58-
}
59-
60-
@Test
61-
void useDefaults() {
62-
Resource resource = new ByteArrayResource("banner ${b:def1} ${c:def2} ${d:def3}".getBytes());
63-
String banner = printBanner(resource, null, null, null);
64-
assertThat(banner).startsWith("banner def1 def2 def3");
65-
}
66-
6752
@Test
6853
void renderVersions() {
6954
Resource resource = new ByteArrayResource(
@@ -142,6 +127,15 @@ void renderWithoutTitle() {
142127
assertThat(banner).startsWith("banner 1");
143128
}
144129

130+
@Test
131+
void renderWithDefaultValues() {
132+
Resource resource = new ByteArrayResource(
133+
"banner ${a:default-a} ${b:default-b} ${spring-boot.version:default-boot-version} ${application.version:default-application-version}"
134+
.getBytes());
135+
String banner = printBanner(resource, "10.2", "1.0", null);
136+
assertThat(banner).startsWith("banner 1 default-b 10.2 1.0");
137+
}
138+
145139
private String printBanner(Resource resource, String bootVersion, String applicationVersion,
146140
String applicationTitle) {
147141
ResourceBanner banner = new MockResourceBanner(resource, bootVersion, applicationVersion, applicationTitle);

0 commit comments

Comments
 (0)