1
1
/*
2
- * Copyright 2012-2024 the original author or authors.
2
+ * Copyright 2012-2025 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -86,22 +86,34 @@ public void printBanner(Environment environment, Class<?> sourceClass, PrintStre
86
86
* @return a mutable list of property resolvers
87
87
*/
88
88
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 ));
96
89
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 )));
98
92
return resolvers ;
99
93
}
100
94
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 ) {
102
114
String applicationTitle = getApplicationTitle (sourceClass );
103
115
Map <String , Object > titleMap = Collections .singletonMap ("application.title" ,
104
- (applicationTitle != null ) ? applicationTitle : "" );
116
+ (applicationTitle != null ) ? applicationTitle : defaultValue );
105
117
return new MapPropertySource ("title" , titleMap );
106
118
}
107
119
@@ -120,21 +132,21 @@ private AnsiPropertySource getAnsiSource() {
120
132
return new AnsiPropertySource ("ansi" , true );
121
133
}
122
134
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 ));
125
137
}
126
138
127
- private Map <String , Object > getVersionsMap (Class <?> sourceClass , Environment environment ) {
139
+ private Map <String , Object > getVersionsMap (Class <?> sourceClass , Environment environment , String defaultValue ) {
128
140
String appVersion = getApplicationVersion (sourceClass );
129
141
if (appVersion == null ) {
130
142
appVersion = getApplicationVersion (environment );
131
143
}
132
144
String bootVersion = getBootVersion ();
133
145
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 ));
138
150
return versions ;
139
151
}
140
152
@@ -157,9 +169,9 @@ protected String getBootVersion() {
157
169
return SpringBootVersion .getVersion ();
158
170
}
159
171
160
- private String getVersionString (String version , boolean format ) {
172
+ private String getVersionString (String version , boolean format , String fallback ) {
161
173
if (version == null ) {
162
- return "" ;
174
+ return fallback ;
163
175
}
164
176
return format ? " (v" + version + ")" : version ;
165
177
}
0 commit comments