Skip to content

Commit 133304c

Browse files
committed
Resolve placeholders in banner through Enviornment
see #1191
1 parent e9c69aa commit 133304c

File tree

3 files changed

+37
-22
lines changed

3 files changed

+37
-22
lines changed

spring-boot/src/main/java/org/springframework/boot/SpringApplication.java

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -77,68 +77,69 @@
7777
* Classes that can be used to bootstrap and launch a Spring application from a Java main
7878
* method. By default class will perform the following steps to bootstrap your
7979
* application:
80-
*
80+
*
8181
* <ul>
8282
* <li>Create an appropriate {@link ApplicationContext} instance (depending on your
8383
* classpath)</li>
84-
*
84+
*
8585
* <li>Register a {@link CommandLinePropertySource} to expose command line arguments as
8686
* Spring properties</li>
87-
*
87+
*
8888
* <li>Refresh the application context, loading all singleton beans</li>
89-
*
89+
*
9090
* <li>Trigger any {@link CommandLineRunner} beans</li>
9191
* </ul>
92-
*
92+
*
9393
* In most circumstances the static {@link #run(Object, String[])} method can be called
9494
* directly from your {@literal main} method to bootstrap your application:
95-
*
95+
*
9696
* <pre class="code">
9797
* &#064;Configuration
9898
* &#064;EnableAutoConfiguration
9999
* public class MyApplication {
100-
*
100+
*
101101
* // ... Bean definitions
102-
*
102+
*
103103
* public static void main(String[] args) throws Exception {
104104
* SpringApplication.run(MyApplication.class, args);
105105
* }
106106
* </pre>
107-
*
107+
*
108108
* <p>
109109
* For more advanced configuration a {@link SpringApplication} instance can be created and
110110
* customized before being run:
111-
*
111+
*
112112
* <pre class="code">
113113
* public static void main(String[] args) throws Exception {
114114
* SpringApplication app = new SpringApplication(MyApplication.class);
115115
* // ... customize app settings here
116116
* app.run(args)
117117
* }
118118
* </pre>
119-
*
119+
*
120120
* {@link SpringApplication}s can read beans from a variety of different sources. It is
121121
* generally recommended that a single {@code @Configuration} class is used to bootstrap
122122
* your application, however, any of the following sources can also be used:
123-
*
123+
*
124124
* <p>
125125
* <ul>
126126
* <li>{@link Class} - A Java class to be loaded by {@link AnnotatedBeanDefinitionReader}</li>
127-
*
127+
*
128128
* <li>{@link Resource} - An XML resource to be loaded by {@link XmlBeanDefinitionReader},
129129
* or a groovy script to be loaded by {@link GroovyBeanDefinitionReader}</li>
130-
*
130+
*
131131
* <li>{@link Package} - A Java package to be scanned by
132132
* {@link ClassPathBeanDefinitionScanner}</li>
133-
*
133+
*
134134
* <li>{@link CharSequence} - A class name, resource handle or package name to loaded as
135135
* appropriate. If the {@link CharSequence} cannot be resolved to class and does not
136136
* resolve to a {@link Resource} that exists it will be considered a {@link Package}.</li>
137137
* </ul>
138-
*
138+
*
139139
* @author Phillip Webb
140140
* @author Dave Syer
141141
* @author Andy Wilkinson
142+
* @author Christian Dupuis
142143
* @see #run(Object, String[])
143144
* @see #run(Object[], String[])
144145
* @see #SpringApplication(Object...)
@@ -152,7 +153,7 @@ public class SpringApplication {
152153
+ "boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext";
153154

154155
private static final String[] WEB_ENVIRONMENT_CLASSES = { "javax.servlet.Servlet",
155-
"org.springframework.web.context.ConfigurableWebApplicationContext" };
156+
"org.springframework.web.context.ConfigurableWebApplicationContext" };
156157

157158
private static final String SYSTEM_PROPERTY_JAVA_AWT_HEADLESS = "java.awt.headless";
158159

@@ -478,10 +479,11 @@ protected void printBanner(Environment environment) {
478479
Resource resource = resourceLoader.getResource(location);
479480
if (resource.exists()) {
480481
try {
481-
System.out.println(StreamUtils.copyToString(
482+
String banner = StreamUtils.copyToString(
482483
resource.getInputStream(),
483484
environment.getProperty("banner.charset", Charset.class,
484-
Charset.forName("UTF-8"))));
485+
Charset.forName("UTF-8")));
486+
System.out.println(environment.resolvePlaceholders(banner));
485487
return;
486488
}
487489
catch (Exception ex) {
@@ -546,7 +548,7 @@ protected void postProcessApplicationContext(ConfigurableApplicationContext cont
546548
if (this.resourceLoader != null) {
547549
if (context instanceof GenericApplicationContext) {
548550
((GenericApplicationContext) context)
549-
.setResourceLoader(this.resourceLoader);
551+
.setResourceLoader(this.resourceLoader);
550552
}
551553
if (context instanceof DefaultResourceLoader) {
552554
((DefaultResourceLoader) context).setClassLoader(this.resourceLoader
@@ -579,7 +581,7 @@ protected void applyInitializers(ConfigurableApplicationContext context) {
579581
protected void logStartupInfo(boolean isRoot) {
580582
if (isRoot) {
581583
new StartupInfoLogger(this.mainApplicationClass)
582-
.logStarting(getApplicationLog());
584+
.logStarting(getApplicationLog());
583585
}
584586
}
585587

spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,11 @@
7979

8080
/**
8181
* Tests for {@link SpringApplication}.
82-
*
82+
*
8383
* @author Phillip Webb
8484
* @author Dave Syer
8585
* @author Andy Wilkinson
86+
* @author Christian Dupuis
8687
*/
8788
public class SpringApplicationTests {
8889

@@ -162,6 +163,15 @@ public void customBanner() throws Exception {
162163
verify(application, never()).printBanner();
163164
}
164165

166+
@Test
167+
public void customBannerWithProperties() throws Exception {
168+
SpringApplication application = spy(new SpringApplication(ExampleConfig.class));
169+
application.setWebEnvironment(false);
170+
application.run("--banner.location=classpath:test-banner-with-placeholder.txt",
171+
"--test.property=123456");
172+
verify(application, never()).printBanner();
173+
}
174+
165175
@Test
166176
public void customId() throws Exception {
167177
SpringApplication application = new SpringApplication(ExampleConfig.class);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Running a Test!
2+
3+
${test.property}

0 commit comments

Comments
 (0)