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.
@@ -85,22 +85,34 @@ public void printBanner(Environment environment, Class<?> sourceClass, PrintStre
85
85
* @return a mutable list of property resolvers
86
86
*/
87
87
protected List <PropertyResolver > getPropertyResolvers (Environment environment , Class <?> sourceClass ) {
88
- MutablePropertySources sources = new MutablePropertySources ();
89
- if (environment instanceof ConfigurableEnvironment configurableEnvironment ) {
90
- configurableEnvironment .getPropertySources ().forEach (sources ::addLast );
91
- }
92
- sources .addLast (getTitleSource (sourceClass ));
93
- sources .addLast (getAnsiSource ());
94
- sources .addLast (getVersionSource (sourceClass ));
95
88
List <PropertyResolver > resolvers = new ArrayList <>();
96
- resolvers .add (new PropertySourcesPropertyResolver (sources ));
89
+ resolvers .add (new PropertySourcesPropertyResolver (createNullDefaultSources (environment , sourceClass )));
90
+ resolvers .add (new PropertySourcesPropertyResolver (createEmptyDefaultSources (sourceClass )));
97
91
return resolvers ;
98
92
}
99
93
100
- private MapPropertySource getTitleSource (Class <?> sourceClass ) {
94
+ private MutablePropertySources createNullDefaultSources (Environment environment , Class <?> sourceClass ) {
95
+ MutablePropertySources nullDefaultSources = new MutablePropertySources ();
96
+ if (environment instanceof ConfigurableEnvironment configurableEnvironment ) {
97
+ configurableEnvironment .getPropertySources ().forEach (nullDefaultSources ::addLast );
98
+ }
99
+ nullDefaultSources .addLast (getTitleSource (sourceClass , null ));
100
+ nullDefaultSources .addLast (getAnsiSource ());
101
+ nullDefaultSources .addLast (getVersionSource (sourceClass , null ));
102
+ return nullDefaultSources ;
103
+ }
104
+
105
+ private MutablePropertySources createEmptyDefaultSources (Class <?> sourceClass ) {
106
+ MutablePropertySources emptyDefaultSources = new MutablePropertySources ();
107
+ emptyDefaultSources .addLast (getTitleSource (sourceClass , "" ));
108
+ emptyDefaultSources .addLast (getVersionSource (sourceClass , "" ));
109
+ return emptyDefaultSources ;
110
+ }
111
+
112
+ private MapPropertySource getTitleSource (Class <?> sourceClass , String defaultValue ) {
101
113
String applicationTitle = getApplicationTitle (sourceClass );
102
114
Map <String , Object > titleMap = Collections .singletonMap ("application.title" ,
103
- (applicationTitle != null ) ? applicationTitle : "" );
115
+ (applicationTitle != null ) ? applicationTitle : defaultValue );
104
116
return new MapPropertySource ("title" , titleMap );
105
117
}
106
118
@@ -119,18 +131,18 @@ private AnsiPropertySource getAnsiSource() {
119
131
return new AnsiPropertySource ("ansi" , true );
120
132
}
121
133
122
- private MapPropertySource getVersionSource (Class <?> sourceClass ) {
123
- return new MapPropertySource ("version" , getVersionsMap (sourceClass ));
134
+ private MapPropertySource getVersionSource (Class <?> sourceClass , String defaultValue ) {
135
+ return new MapPropertySource ("version" , getVersionsMap (sourceClass , defaultValue ));
124
136
}
125
137
126
- private Map <String , Object > getVersionsMap (Class <?> sourceClass ) {
138
+ private Map <String , Object > getVersionsMap (Class <?> sourceClass , String defaultValue ) {
127
139
String appVersion = getApplicationVersion (sourceClass );
128
140
String bootVersion = getBootVersion ();
129
141
Map <String , Object > versions = new HashMap <>();
130
- versions .put ("application.version" , getVersionString (appVersion , false ));
131
- versions .put ("spring-boot.version" , getVersionString (bootVersion , false ));
132
- versions .put ("application.formatted-version" , getVersionString (appVersion , true ));
133
- versions .put ("spring-boot.formatted-version" , getVersionString (bootVersion , true ));
142
+ versions .put ("application.version" , getVersionString (appVersion , false , defaultValue ));
143
+ versions .put ("spring-boot.version" , getVersionString (bootVersion , false , defaultValue ));
144
+ versions .put ("application.formatted-version" , getVersionString (appVersion , true , defaultValue ));
145
+ versions .put ("spring-boot.formatted-version" , getVersionString (bootVersion , true , defaultValue ));
134
146
return versions ;
135
147
}
136
148
@@ -143,9 +155,9 @@ protected String getBootVersion() {
143
155
return SpringBootVersion .getVersion ();
144
156
}
145
157
146
- private String getVersionString (String version , boolean format ) {
158
+ private String getVersionString (String version , boolean format , String fallback ) {
147
159
if (version == null ) {
148
- return "" ;
160
+ return fallback ;
149
161
}
150
162
return format ? " (v" + version + ")" : version ;
151
163
}
0 commit comments