From 7a33afa72275c9031b5c1f2db260047399199b69 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Wed, 30 Apr 2014 16:30:54 +0100 Subject: [PATCH] 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 --- .../boot/SpringApplication.java | 34 ++++++++++++++++++- .../boot/SpringApplicationTests.java | 4 +-- .../src/test/resources/test-banner.txt | 1 + 3 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 spring-boot/src/test/resources/test-banner.txt diff --git a/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java b/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java index 2e295c3388..d68bba5b1f 100644 --- a/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java +++ b/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java @@ -17,6 +17,7 @@ package org.springframework.boot; import java.lang.reflect.Constructor; +import java.nio.charset.Charset; import java.security.AccessControlException; import java.util.ArrayList; import java.util.Arrays; @@ -66,6 +67,7 @@ import org.springframework.util.Assert; import org.springframework.util.ClassUtils; import org.springframework.util.ReflectionUtils; import org.springframework.util.StopWatch; +import org.springframework.util.StreamUtils; import org.springframework.util.StringUtils; import org.springframework.web.context.ConfigurableWebApplicationContext; import org.springframework.web.context.WebApplicationContext; @@ -276,7 +278,7 @@ public class SpringApplication { } if (this.showBanner) { - printBanner(); + printBanner(environment); } // Create, load, refresh and run the ApplicationContext @@ -452,6 +454,36 @@ public class SpringApplication { 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 * to provide additional or alternative banners. diff --git a/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java b/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java index 3d2fa4d0c9..91c7061768 100644 --- a/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java @@ -123,8 +123,8 @@ public class SpringApplicationTests { public void customBanner() throws Exception { SpringApplication application = spy(new SpringApplication(ExampleConfig.class)); application.setWebEnvironment(false); - application.run(); - verify(application).printBanner(); + application.run("--banner.location=classpath:test-banner.txt"); + verify(application, never()).printBanner(); } @Test diff --git a/spring-boot/src/test/resources/test-banner.txt b/spring-boot/src/test/resources/test-banner.txt new file mode 100644 index 0000000000..d60fc28bde --- /dev/null +++ b/spring-boot/src/test/resources/test-banner.txt @@ -0,0 +1 @@ +Running a Test! \ No newline at end of file