Skip to content

Commit 5c01aa7

Browse files
krzykwilkinsona
authored andcommitted
Fix support for default values in banner placeholders
See gh-34764
1 parent 17a4d30 commit 5c01aa7

File tree

2 files changed

+42
-22
lines changed

2 files changed

+42
-22
lines changed

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

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,7 +19,6 @@
1919
import java.io.PrintStream;
2020
import java.nio.charset.Charset;
2121
import java.nio.charset.StandardCharsets;
22-
import java.util.ArrayList;
2322
import java.util.Collections;
2423
import java.util.HashMap;
2524
import java.util.List;
@@ -33,6 +32,7 @@
3332
import org.springframework.core.env.MapPropertySource;
3433
import org.springframework.core.env.MutablePropertySources;
3534
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;
@@ -45,13 +45,14 @@
4545
* @author Phillip Webb
4646
* @author Vedran Pavic
4747
* @author Toshiaki Maki
48+
* @author Krzysztof Krason
4849
* @since 1.2.0
4950
*/
5051
public class ResourceBanner implements Banner {
5152

5253
private static final Log logger = LogFactory.getLog(ResourceBanner.class);
5354

54-
private Resource resource;
55+
private final Resource resource;
5556

5657
public ResourceBanner(Resource resource) {
5758
Assert.notNull(resource, "Resource must not be null");
@@ -77,18 +78,25 @@ public void printBanner(Environment environment, Class<?> sourceClass, PrintStre
7778
}
7879

7980
protected List<PropertyResolver> getPropertyResolvers(Environment environment, Class<?> sourceClass) {
80-
List<PropertyResolver> resolvers = new ArrayList<>();
81-
resolvers.add(environment);
82-
resolvers.add(getVersionResolver(sourceClass));
83-
resolvers.add(getAnsiResolver());
84-
resolvers.add(getTitleResolver(sourceClass));
85-
return resolvers;
81+
MutablePropertySources propertySources = new MutablePropertySources();
82+
propertySources.addLast(getEnvironmentSource(environment));
83+
propertySources.addLast(getTitleSource(sourceClass));
84+
propertySources.addLast(getAnsiSource());
85+
propertySources.addLast(getVersionSource(sourceClass));
86+
return Collections.singletonList(new PropertySourcesPropertyResolver(propertySources));
8687
}
8788

88-
private PropertyResolver getVersionResolver(Class<?> sourceClass) {
89-
MutablePropertySources propertySources = new MutablePropertySources();
90-
propertySources.addLast(new MapPropertySource("version", getVersionsMap(sourceClass)));
91-
return new PropertySourcesPropertyResolver(propertySources);
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+
98+
private MapPropertySource getVersionSource(Class<?> sourceClass) {
99+
return new MapPropertySource("version", getVersionsMap(sourceClass));
92100
}
93101

94102
private Map<String, Object> getVersionsMap(Class<?> sourceClass) {
@@ -118,19 +126,15 @@ private String getVersionString(String version, boolean format) {
118126
return format ? " (v" + version + ")" : version;
119127
}
120128

121-
private PropertyResolver getAnsiResolver() {
122-
MutablePropertySources sources = new MutablePropertySources();
123-
sources.addFirst(new AnsiPropertySource("ansi", true));
124-
return new PropertySourcesPropertyResolver(sources);
129+
private AnsiPropertySource getAnsiSource() {
130+
return new AnsiPropertySource("ansi", true);
125131
}
126132

127-
private PropertyResolver getTitleResolver(Class<?> sourceClass) {
128-
MutablePropertySources sources = new MutablePropertySources();
133+
private MapPropertySource getTitleSource(Class<?> sourceClass) {
129134
String applicationTitle = getApplicationTitle(sourceClass);
130135
Map<String, Object> titleMap = Collections.singletonMap("application.title",
131136
(applicationTitle != null) ? applicationTitle : "");
132-
sources.addFirst(new MapPropertySource("title", titleMap));
133-
return new PropertySourcesPropertyResolver(sources);
137+
return new MapPropertySource("title", titleMap);
134138
}
135139

136140
protected String getApplicationTitle(Class<?> sourceClass) {

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -40,6 +40,7 @@
4040
* @author Phillip Webb
4141
* @author Vedran Pavic
4242
* @author Toshiaki Maki
43+
* @author Krzysztof Krason
4344
*/
4445
class ResourceBannerTests {
4546

@@ -48,6 +49,21 @@ void reset() {
4849
AnsiOutput.setEnabled(Enabled.DETECT);
4950
}
5051

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+
5167
@Test
5268
void renderVersions() {
5369
Resource resource = new ByteArrayResource(

0 commit comments

Comments
 (0)