Add support for custom banner
SpringApplication now picks up a classpath:banner.txt by default, and user can choose a different one with banner.location. The encoding is banner.encoding. Fixes gh-458
This commit is contained in:
parent
537b0c3ff3
commit
7a33afa722
|
|
@ -17,6 +17,7 @@
|
||||||
package org.springframework.boot;
|
package org.springframework.boot;
|
||||||
|
|
||||||
import java.lang.reflect.Constructor;
|
import java.lang.reflect.Constructor;
|
||||||
|
import java.nio.charset.Charset;
|
||||||
import java.security.AccessControlException;
|
import java.security.AccessControlException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
@ -66,6 +67,7 @@ import org.springframework.util.Assert;
|
||||||
import org.springframework.util.ClassUtils;
|
import org.springframework.util.ClassUtils;
|
||||||
import org.springframework.util.ReflectionUtils;
|
import org.springframework.util.ReflectionUtils;
|
||||||
import org.springframework.util.StopWatch;
|
import org.springframework.util.StopWatch;
|
||||||
|
import org.springframework.util.StreamUtils;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
import org.springframework.web.context.ConfigurableWebApplicationContext;
|
import org.springframework.web.context.ConfigurableWebApplicationContext;
|
||||||
import org.springframework.web.context.WebApplicationContext;
|
import org.springframework.web.context.WebApplicationContext;
|
||||||
|
|
@ -276,7 +278,7 @@ public class SpringApplication {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.showBanner) {
|
if (this.showBanner) {
|
||||||
printBanner();
|
printBanner(environment);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create, load, refresh and run the ApplicationContext
|
// Create, load, refresh and run the ApplicationContext
|
||||||
|
|
@ -452,6 +454,36 @@ public class SpringApplication {
|
||||||
environment.setActiveProfiles(profiles.toArray(new String[profiles.size()]));
|
environment.setActiveProfiles(profiles.toArray(new String[profiles.size()]));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Print a custom banner message to the console, optionally extracting its location or
|
||||||
|
* content from the Environment (banner.location and banner.charset). The defaults are
|
||||||
|
* banner.location=classpath:banner.txt, banner.charest=UTF-8. If the banner file does
|
||||||
|
* not exist or cannot be printed, a simple default is created.
|
||||||
|
*
|
||||||
|
* @see #setShowBanner(boolean)
|
||||||
|
* @see #printBanner()
|
||||||
|
*/
|
||||||
|
protected void printBanner(Environment environment) {
|
||||||
|
String location = environment.getProperty("banner.location", "banner.txt");
|
||||||
|
ResourceLoader resourceLoader = this.resourceLoader != null ? this.resourceLoader
|
||||||
|
: new DefaultResourceLoader(getClassLoader());
|
||||||
|
Resource resource = resourceLoader.getResource(location);
|
||||||
|
if (resource.exists()) {
|
||||||
|
try {
|
||||||
|
System.out.println(StreamUtils.copyToString(
|
||||||
|
resource.getInputStream(),
|
||||||
|
environment.getProperty("banner.charset", Charset.class,
|
||||||
|
Charset.forName("UTF-8"))));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
System.out.println("Banner not printable: " + resource + " ("
|
||||||
|
+ e.getClass() + ": '" + e.getMessage() + "')");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
printBanner();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Print a simple banner message to the console. Subclasses can override this method
|
* Print a simple banner message to the console. Subclasses can override this method
|
||||||
* to provide additional or alternative banners.
|
* to provide additional or alternative banners.
|
||||||
|
|
|
||||||
|
|
@ -123,8 +123,8 @@ public class SpringApplicationTests {
|
||||||
public void customBanner() throws Exception {
|
public void customBanner() throws Exception {
|
||||||
SpringApplication application = spy(new SpringApplication(ExampleConfig.class));
|
SpringApplication application = spy(new SpringApplication(ExampleConfig.class));
|
||||||
application.setWebEnvironment(false);
|
application.setWebEnvironment(false);
|
||||||
application.run();
|
application.run("--banner.location=classpath:test-banner.txt");
|
||||||
verify(application).printBanner();
|
verify(application, never()).printBanner();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
Running a Test!
|
||||||
Loading…
Reference in New Issue