@@ -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 }
0 commit comments