Skip to content

Commit 7241352

Browse files
committed
Merge branch '3.4.x'
Closes gh-44256
2 parents 64b1e56 + 885fcdb commit 7241352

File tree

2 files changed

+58
-22
lines changed

2 files changed

+58
-22
lines changed

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

+31-19
Original file line numberDiff line numberDiff line change
@@ -86,22 +86,34 @@ public void printBanner(Environment environment, Class<?> sourceClass, PrintStre
8686
* @return a mutable list of property resolvers
8787
*/
8888
protected List<PropertyResolver> getPropertyResolvers(Environment environment, Class<?> sourceClass) {
89-
MutablePropertySources sources = new MutablePropertySources();
90-
if (environment instanceof ConfigurableEnvironment configurableEnvironment) {
91-
configurableEnvironment.getPropertySources().forEach(sources::addLast);
92-
}
93-
sources.addLast(getTitleSource(sourceClass));
94-
sources.addLast(getAnsiSource());
95-
sources.addLast(getVersionSource(sourceClass, environment));
9689
List<PropertyResolver> resolvers = new ArrayList<>();
97-
resolvers.add(new PropertySourcesPropertyResolver(sources));
90+
resolvers.add(new PropertySourcesPropertyResolver(createNullDefaultSources(environment, sourceClass)));
91+
resolvers.add(new PropertySourcesPropertyResolver(createEmptyDefaultSources(environment, sourceClass)));
9892
return resolvers;
9993
}
10094

101-
private MapPropertySource getTitleSource(Class<?> sourceClass) {
95+
private MutablePropertySources createNullDefaultSources(Environment environment, Class<?> sourceClass) {
96+
MutablePropertySources nullDefaultSources = new MutablePropertySources();
97+
if (environment instanceof ConfigurableEnvironment configurableEnvironment) {
98+
configurableEnvironment.getPropertySources().forEach(nullDefaultSources::addLast);
99+
}
100+
nullDefaultSources.addLast(getTitleSource(sourceClass, null));
101+
nullDefaultSources.addLast(getAnsiSource());
102+
nullDefaultSources.addLast(getVersionSource(sourceClass, environment, null));
103+
return nullDefaultSources;
104+
}
105+
106+
private MutablePropertySources createEmptyDefaultSources(Environment environment, Class<?> sourceClass) {
107+
MutablePropertySources emptyDefaultSources = new MutablePropertySources();
108+
emptyDefaultSources.addLast(getTitleSource(sourceClass, ""));
109+
emptyDefaultSources.addLast(getVersionSource(sourceClass, environment, ""));
110+
return emptyDefaultSources;
111+
}
112+
113+
private MapPropertySource getTitleSource(Class<?> sourceClass, String defaultValue) {
102114
String applicationTitle = getApplicationTitle(sourceClass);
103115
Map<String, Object> titleMap = Collections.singletonMap("application.title",
104-
(applicationTitle != null) ? applicationTitle : "");
116+
(applicationTitle != null) ? applicationTitle : defaultValue);
105117
return new MapPropertySource("title", titleMap);
106118
}
107119

@@ -120,21 +132,21 @@ private AnsiPropertySource getAnsiSource() {
120132
return new AnsiPropertySource("ansi", true);
121133
}
122134

123-
private MapPropertySource getVersionSource(Class<?> sourceClass, Environment environment) {
124-
return new MapPropertySource("version", getVersionsMap(sourceClass, environment));
135+
private MapPropertySource getVersionSource(Class<?> sourceClass, Environment environment, String defaultValue) {
136+
return new MapPropertySource("version", getVersionsMap(sourceClass, environment, defaultValue));
125137
}
126138

127-
private Map<String, Object> getVersionsMap(Class<?> sourceClass, Environment environment) {
139+
private Map<String, Object> getVersionsMap(Class<?> sourceClass, Environment environment, String defaultValue) {
128140
String appVersion = getApplicationVersion(sourceClass);
129141
if (appVersion == null) {
130142
appVersion = getApplicationVersion(environment);
131143
}
132144
String bootVersion = getBootVersion();
133145
Map<String, Object> versions = new HashMap<>();
134-
versions.put("application.version", getVersionString(appVersion, false));
135-
versions.put("spring-boot.version", getVersionString(bootVersion, false));
136-
versions.put("application.formatted-version", getVersionString(appVersion, true));
137-
versions.put("spring-boot.formatted-version", getVersionString(bootVersion, true));
146+
versions.put("application.version", getVersionString(appVersion, false, defaultValue));
147+
versions.put("spring-boot.version", getVersionString(bootVersion, false, defaultValue));
148+
versions.put("application.formatted-version", getVersionString(appVersion, true, defaultValue));
149+
versions.put("spring-boot.formatted-version", getVersionString(bootVersion, true, defaultValue));
138150
return versions;
139151
}
140152

@@ -157,9 +169,9 @@ protected String getBootVersion() {
157169
return SpringBootVersion.getVersion();
158170
}
159171

160-
private String getVersionString(String version, boolean format) {
172+
private String getVersionString(String version, boolean format, String fallback) {
161173
if (version == null) {
162-
return "";
174+
return fallback;
163175
}
164176
return format ? " (v" + version + ")" : version;
165177
}

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

+27-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 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.
@@ -68,6 +68,14 @@ void renderWithoutVersions() {
6868
assertThat(banner).startsWith("banner 1 ");
6969
}
7070

71+
@Test
72+
void renderWithoutVersionsWithDefaultValues() {
73+
Resource resource = new ByteArrayResource(
74+
"banner ${a} ${spring-boot.version:X.Y.Z} ${application.version:A.B.C}".getBytes());
75+
String banner = printBanner(resource, null, null, null);
76+
assertThat(banner).startsWith("banner 1 X.Y.Z A.B.C");
77+
}
78+
7179
@Test
7280
void renderFormattedVersions() {
7381
Resource resource = new ByteArrayResource(
@@ -79,9 +87,18 @@ void renderFormattedVersions() {
7987
@Test
8088
void renderWithoutFormattedVersions() {
8189
Resource resource = new ByteArrayResource(
82-
"banner ${a}${spring-boot.formatted-version}${application.formatted-version}".getBytes());
90+
"banner ${a} ${spring-boot.formatted-version} ${application.formatted-version}".getBytes());
8391
String banner = printBanner(resource, null, null, null);
84-
assertThat(banner).startsWith("banner 1");
92+
assertThat(banner).startsWith("banner 1 ");
93+
}
94+
95+
@Test
96+
void renderWithoutFormattedVersionsWithDefaultValues() {
97+
Resource resource = new ByteArrayResource(
98+
"banner ${a} ${spring-boot.formatted-version:(vX.Y.Z)} ${application.formatted-version:(vA.B.C)}"
99+
.getBytes());
100+
String banner = printBanner(resource, null, null, null);
101+
assertThat(banner).startsWith("banner 1 (vX.Y.Z) (vA.B.C)");
85102
}
86103

87104
@Test
@@ -130,6 +147,13 @@ void renderWithoutTitle() {
130147
assertThat(banner).startsWith("banner 1");
131148
}
132149

150+
@Test
151+
void renderWithoutTitleWithDefaultValue() {
152+
Resource resource = new ByteArrayResource("banner ${application.title:Default Title} ${a}".getBytes());
153+
String banner = printBanner(resource, null, null, null);
154+
assertThat(banner).startsWith("banner Default Title 1");
155+
}
156+
133157
@Test
134158
void renderWithDefaultValues() {
135159
Resource resource = new ByteArrayResource(

0 commit comments

Comments
 (0)