From 30906599712d51242ef2ce7fe0d771af46fa6f3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CJeremy?= Date: Thu, 24 Sep 2015 11:42:04 -0600 Subject: [PATCH 1/2] Add an option to log banner rather than printing it to standard out This commit adds the option to output the banner using the logger instead of standard out. Rather than taking a boolean spring.main.show-banner is now configured using an enum. Three values are supported: - LOG: the banner is logged - CONSOLE: the banner is printed to standard out (previously true) - OFF: the banner is switched off (previously false) The default behavior remains unchanged; the banner will be printed to standard out. Closes gh-4022 See gh-4001 --- spring-boot-docs/src/main/asciidoc/howto.adoc | 6 +-- .../main/asciidoc/spring-boot-features.adoc | 2 +- .../java/org/springframework/boot/Banner.java | 22 +++++++++ .../boot/SpringApplication.java | 47 ++++++++++++------- .../builder/SpringApplicationBuilder.java | 4 +- .../org/springframework/boot/BannerTests.java | 8 ++++ .../springframework/boot/SimpleMainTests.java | 2 +- .../boot/SpringApplicationTests.java | 38 +++++++++++---- ...nfigFileEnvironmentPostProcessorTests.java | 7 +-- .../resources/bindtoapplication.properties | 2 +- 10 files changed, 101 insertions(+), 37 deletions(-) diff --git a/spring-boot-docs/src/main/asciidoc/howto.adoc b/spring-boot-docs/src/main/asciidoc/howto.adoc index 7031be4bf17..d7df7d0960d 100644 --- a/spring-boot-docs/src/main/asciidoc/howto.adoc +++ b/spring-boot-docs/src/main/asciidoc/howto.adoc @@ -123,7 +123,7 @@ might have. [source,properties,indent=0,subs="verbatim,quotes,attributes"] ---- spring.main.web_environment=false - spring.main.show_banner=false + spring.main.show_banner=OFF ---- and then the Spring Boot banner will not be printed on startup, and the application will @@ -139,7 +139,7 @@ consider this application [source,java,indent=0] ---- new SpringApplicationBuilder() - .showBanner(false) + .showBanner(Banner.Mode.OFF) .sources(demo.MyApp.class) .run(args); ---- @@ -149,7 +149,7 @@ used with the following configuration: [source,properties,indent=0,subs="verbatim,quotes,attributes"] ---- spring.main.sources=com.acme.Config,com.acme.ExtraConfig - spring.main.show-banner=true + spring.main.show-banner=CONSOLE ---- The actual application will _now_ show the banner (as overridden by configuration) and use diff --git a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc index eebc128cac2..5e5049835e9 100644 --- a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc +++ b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc @@ -124,7 +124,7 @@ For example: [source,java,indent=0] ---- new SpringApplicationBuilder() - .showBanner(false) + .showBanner(Banner.Mode.OFF) .sources(Parent.class) .child(Application.class) .run(args); diff --git a/spring-boot/src/main/java/org/springframework/boot/Banner.java b/spring-boot/src/main/java/org/springframework/boot/Banner.java index 2b7f6294726..e8f9c857155 100644 --- a/spring-boot/src/main/java/org/springframework/boot/Banner.java +++ b/spring-boot/src/main/java/org/springframework/boot/Banner.java @@ -25,6 +25,7 @@ import org.springframework.core.env.Environment; * * @author Phillip Webb * @author Michael Stummvoll + * @author Jeremy Rickard * @since 1.2.0 */ public interface Banner { @@ -37,4 +38,25 @@ public interface Banner { */ void printBanner(Environment environment, Class sourceClass, PrintStream out); + /** + * An enumeration of possible values for configuring the Banner. + */ + enum Mode { + + /** + * Disable printing of the banner. + */ + OFF, + + /** + * Print the banner to System.out. + */ + CONSOLE, + + /** + * Print the banner to the log file. + */ + LOG + } + } 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 e9337594d32..f87caceb9d9 100644 --- a/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java +++ b/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java @@ -16,6 +16,8 @@ package org.springframework.boot; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; import java.lang.reflect.Constructor; import java.security.AccessControlException; import java.util.ArrayList; @@ -134,6 +136,7 @@ import org.springframework.web.context.support.StandardServletEnvironment; * @author Andy Wilkinson * @author Christian Dupuis * @author Stephane Nicoll + * @author Jeremy Rickard * @see #run(Object, String[]) * @see #run(Object[], String[]) * @see #SpringApplication(Object...) @@ -179,7 +182,7 @@ public class SpringApplication { private Class mainApplicationClass; - private boolean showBanner = true; + private Banner.Mode showBanner = Banner.Mode.CONSOLE; private boolean logStartupInfo = true; @@ -321,7 +324,7 @@ public class SpringApplication { environment = convertToStandardEnvironment(environment); } - if (this.showBanner) { + if (this.showBanner != Banner.Mode.OFF) { printBanner(environment); } @@ -521,27 +524,37 @@ public class SpringApplication { * banner.location=classpath:banner.txt, banner.charset=UTF-8. If the banner file does * not exist or cannot be printed, a simple default is created. * @param environment the environment - * @see #setShowBanner(boolean) + * @see #setShowBanner(org.springframework.boot.Banner.Mode) */ protected void printBanner(Environment environment) { + + Banner selectedBanner = selectBanner(environment); + + if (this.showBanner == Banner.Mode.LOG) { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + selectedBanner.printBanner(environment, this.mainApplicationClass, + new PrintStream(baos)); + this.log.info(baos.toString()); + } + else { + selectedBanner + .printBanner(environment, this.mainApplicationClass, System.out); + } + } + + private Banner selectBanner(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()) { - new ResourceBanner(resource).printBanner(environment, - this.mainApplicationClass, System.out); - return; + return new ResourceBanner(resource); } if (this.banner != null) { - this.banner.printBanner(environment, this.mainApplicationClass, System.out); - return; + return this.banner; } - printDefaultBanner(); - } - - private void printDefaultBanner() { - DEFAULT_BANNER.printBanner(null, this.mainApplicationClass, System.out); + return DEFAULT_BANNER; } /** @@ -840,11 +853,11 @@ public class SpringApplication { /** * Sets if the Spring banner should be displayed when the application runs. Defaults - * to {@code true}. - * @param showBanner if the banner should be shown + * to {@code org.springframework.boot.Banner.Mode.CONSOLE}. + * @param bannerMode if the banner should be shown in log or console, or turned off. */ - public void setShowBanner(boolean showBanner) { - this.showBanner = showBanner; + public void setShowBanner(Banner.Mode bannerMode) { + this.showBanner = bannerMode; } /** diff --git a/spring-boot/src/main/java/org/springframework/boot/builder/SpringApplicationBuilder.java b/spring-boot/src/main/java/org/springframework/boot/builder/SpringApplicationBuilder.java index 55c4f4b5fe5..420ee1b0057 100644 --- a/spring-boot/src/main/java/org/springframework/boot/builder/SpringApplicationBuilder.java +++ b/spring-boot/src/main/java/org/springframework/boot/builder/SpringApplicationBuilder.java @@ -172,7 +172,7 @@ public class SpringApplicationBuilder { web(false); // Probably not interested in multiple banners - showBanner(false); + showBanner(Banner.Mode.OFF); // Make sure sources get copied over this.application.setSources(this.sources); @@ -311,7 +311,7 @@ public class SpringApplicationBuilder { * @param showBanner the flag to set. Default true. * @return the current builder */ - public SpringApplicationBuilder showBanner(boolean showBanner) { + public SpringApplicationBuilder showBanner(Banner.Mode showBanner) { this.application.setShowBanner(showBanner); return this; } diff --git a/spring-boot/src/test/java/org/springframework/boot/BannerTests.java b/spring-boot/src/test/java/org/springframework/boot/BannerTests.java index 0d3d05ee5b8..346db7a31b2 100644 --- a/spring-boot/src/test/java/org/springframework/boot/BannerTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/BannerTests.java @@ -57,6 +57,14 @@ public class BannerTests { assertThat(this.out.toString(), containsString(":: Spring Boot ::")); } + @Test + public void testDefaultBannerInLog() throws Exception { + SpringApplication application = new SpringApplication(Config.class); + application.setWebEnvironment(false); + this.context = application.run(); + assertThat(this.out.toString(), containsString(":: Spring Boot ::")); + } + @Test public void testCustomBanner() throws Exception { SpringApplication application = new SpringApplication(Config.class); diff --git a/spring-boot/src/test/java/org/springframework/boot/SimpleMainTests.java b/spring-boot/src/test/java/org/springframework/boot/SimpleMainTests.java index 93f4cc4da5d..60b46ffcb38 100644 --- a/spring-boot/src/test/java/org/springframework/boot/SimpleMainTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/SimpleMainTests.java @@ -76,7 +76,7 @@ public class SimpleMainTests { private String[] getArgs(String... args) { List list = new ArrayList(Arrays.asList( - "--spring.main.webEnvironment=false", "--spring.main.showBanner=false", + "--spring.main.webEnvironment=false", "--spring.main.showBanner=OFF", "--spring.main.registerShutdownHook=false")); if (args.length > 0) { list.add("--spring.main.sources=" 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 8e068643d8a..40ac559125b 100644 --- a/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java @@ -82,6 +82,7 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; import static org.mockito.Matchers.anyObject; +import static org.mockito.Mockito.atLeastOnce; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.spy; @@ -95,6 +96,7 @@ import static org.mockito.Mockito.verify; * @author Andy Wilkinson * @author Christian Dupuis * @author Stephane Nicoll + * @author Jeremy Rickard */ public class SpringApplicationTests { @@ -164,7 +166,7 @@ public class SpringApplicationTests { public void disableBanner() throws Exception { SpringApplication application = spy(new SpringApplication(ExampleConfig.class)); application.setWebEnvironment(false); - application.setShowBanner(false); + application.setShowBanner(Banner.Mode.OFF); this.context = application.run(); verify(application, never()).printBanner((Environment) anyObject()); } @@ -173,7 +175,7 @@ public class SpringApplicationTests { public void disableBannerViaProperty() throws Exception { SpringApplication application = spy(new SpringApplication(ExampleConfig.class)); application.setWebEnvironment(false); - this.context = application.run("--spring.main.show_banner=false"); + this.context = application.run("--spring.main.show_banner=OFF"); verify(application, never()).printBanner((Environment) anyObject()); } @@ -213,6 +215,24 @@ public class SpringApplicationTests { containsString("The following profiles are active: myprofile")); } + @Test + public void enableBannerInLogViaProperty() throws Exception { + SpringApplication application = spy(new SpringApplication(ExampleConfig.class)); + application.setWebEnvironment(false); + this.context = application.run("--spring.main.show_banner=LOG"); + verify(application, atLeastOnce()).setShowBanner(Banner.Mode.LOG); + } + + @Test + public void verifyBannerOutputContainsLogInfo() throws Exception { + SpringApplication application = spy(new SpringApplication(ExampleConfig.class)); + application.setWebEnvironment(false); + application.run("--spring.main.show_banner=LOG", + "--banner.location=classpath:test-banner.txt"); + verify(application, atLeastOnce()).setShowBanner(Banner.Mode.LOG); + assertThat(this.output.toString(), containsString("o.s.boot.SpringApplication")); + } + @Test public void customId() throws Exception { SpringApplication application = new SpringApplication(ExampleConfig.class); @@ -545,8 +565,8 @@ public class SpringApplicationTests { TestSpringApplication application = new TestSpringApplication( ExampleConfig.class); application.setWebEnvironment(false); - this.context = application.run("--spring.main.show_banner=false"); - assertThat(application.getShowBanner(), is(false)); + this.context = application.run("--spring.main.show_banner=OFF"); + assertThat(application.getShowBanner(), is(Banner.Mode.OFF)); } @Test @@ -713,7 +733,7 @@ public class SpringApplicationTests { private boolean useMockLoader; - private boolean showBanner; + private Banner.Mode showBanner; TestSpringApplication(Object... sources) { super(sources); @@ -744,12 +764,12 @@ public class SpringApplicationTests { } @Override - public void setShowBanner(boolean showBanner) { - super.setShowBanner(showBanner); - this.showBanner = showBanner; + public void setShowBanner(Banner.Mode bannerMode) { + super.setShowBanner(bannerMode); + this.showBanner = bannerMode; } - public boolean getShowBanner() { + public Banner.Mode getShowBanner() { return this.showBanner; } diff --git a/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileEnvironmentPostProcessorTests.java b/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileEnvironmentPostProcessorTests.java index 2d9d4b8b6b3..a8c7fa003da 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileEnvironmentPostProcessorTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileEnvironmentPostProcessorTests.java @@ -34,6 +34,7 @@ import org.junit.After; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; +import org.springframework.boot.Banner; import org.springframework.boot.SpringApplication; import org.springframework.boot.context.config.ConfigFileEnvironmentPostProcessor.ConfigurationPropertySources; import org.springframework.boot.env.EnumerableCompositePropertySource; @@ -625,17 +626,17 @@ public class ConfigFileEnvironmentPostProcessorTests { this.initializer.postProcessEnvironment(this.environment, this.application); Field field = ReflectionUtils.findField(SpringApplication.class, "showBanner"); field.setAccessible(true); - assertThat((Boolean) field.get(this.application), equalTo(false)); + assertThat((Banner.Mode) field.get(this.application), equalTo(Banner.Mode.OFF)); } @Test public void bindsSystemPropertyToSpringApplication() throws Exception { // gh-951 - System.setProperty("spring.main.showBanner", "false"); + System.setProperty("spring.main.showBanner", "OFF"); this.initializer.postProcessEnvironment(this.environment, this.application); Field field = ReflectionUtils.findField(SpringApplication.class, "showBanner"); field.setAccessible(true); - assertThat((Boolean) field.get(this.application), equalTo(false)); + assertThat((Banner.Mode) field.get(this.application), equalTo(Banner.Mode.OFF)); } private static Matcher containsPropertySource( diff --git a/spring-boot/src/test/resources/bindtoapplication.properties b/spring-boot/src/test/resources/bindtoapplication.properties index 322672defd4..ec0094f3fda 100644 --- a/spring-boot/src/test/resources/bindtoapplication.properties +++ b/spring-boot/src/test/resources/bindtoapplication.properties @@ -1 +1 @@ -spring.main.show_banner=false +spring.main.show_banner=OFF From 01eb4cf95406b6865bee855f657cf0ebc090fd47 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Thu, 8 Oct 2015 10:44:16 +0100 Subject: [PATCH 2/2] Rework breaking API changes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit changes the new mode-based configuration to use two new methods – setBannerMode on SpringApplication and bannerMode on SpringApplicationBuilder. The old methods, setShowBanner and showBanner on SpringApplication and SpringApplicationBuilder respectively, have been reinstated and deprecated. Closes gh-4001 --- .../appendix-application-properties.adoc | 1 + spring-boot-docs/src/main/asciidoc/howto.adoc | 6 +- .../main/asciidoc/spring-boot-features.adoc | 4 +- .../boot/SpringApplication.java | 50 ++++++++++++----- .../builder/SpringApplicationBuilder.java | 11 +++- .../boot/SpringApplicationTests.java | 55 +++++++++++-------- ...nfigFileEnvironmentPostProcessorTests.java | 8 +-- .../resources/bindtoapplication.properties | 2 +- 8 files changed, 88 insertions(+), 49 deletions(-) diff --git a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc index 58fabb5c473..b7af65f8a4c 100644 --- a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc +++ b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc @@ -43,6 +43,7 @@ content into your application; rather pick only the properties that you need. spring.main.sources= # sources (class name, package name or XML resource location) to include spring.main.web-environment= # detect by default spring.main.show-banner=true + spring.main.banner-mode=console # the mode used to display the banner (console, off, or log) spring.main....= # see class for all properties # AUTO-CONFIGURATION diff --git a/spring-boot-docs/src/main/asciidoc/howto.adoc b/spring-boot-docs/src/main/asciidoc/howto.adoc index d7df7d0960d..ed48010ecfd 100644 --- a/spring-boot-docs/src/main/asciidoc/howto.adoc +++ b/spring-boot-docs/src/main/asciidoc/howto.adoc @@ -123,7 +123,7 @@ might have. [source,properties,indent=0,subs="verbatim,quotes,attributes"] ---- spring.main.web_environment=false - spring.main.show_banner=OFF + spring.main.banner_mode=off ---- and then the Spring Boot banner will not be printed on startup, and the application will @@ -139,7 +139,7 @@ consider this application [source,java,indent=0] ---- new SpringApplicationBuilder() - .showBanner(Banner.Mode.OFF) + .bannerMode(Banner.Mode.OFF) .sources(demo.MyApp.class) .run(args); ---- @@ -149,7 +149,7 @@ used with the following configuration: [source,properties,indent=0,subs="verbatim,quotes,attributes"] ---- spring.main.sources=com.acme.Config,com.acme.ExtraConfig - spring.main.show-banner=CONSOLE + spring.main.banner_mode=console ---- The actual application will _now_ show the banner (as overridden by configuration) and use diff --git a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc index 5e5049835e9..43c6bf5b9cb 100644 --- a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc +++ b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc @@ -93,7 +93,7 @@ instance and customize it. For example, to turn off the banner you would write: ---- public static void main(String[] args) { SpringApplication app = new SpringApplication(MySpringConfiguration.class); - app.setShowBanner(false); + app.setBannerMode(Banner.Mode.OFF); app.run(args); } ---- @@ -124,7 +124,7 @@ For example: [source,java,indent=0] ---- new SpringApplicationBuilder() - .showBanner(Banner.Mode.OFF) + .bannerMode(Banner.Mode.OFF) .sources(Parent.class) .child(Application.class) .run(args); 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 f87caceb9d9..fce9d4191c7 100644 --- a/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java +++ b/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java @@ -18,6 +18,7 @@ package org.springframework.boot; import java.io.ByteArrayOutputStream; import java.io.PrintStream; +import java.io.UnsupportedEncodingException; import java.lang.reflect.Constructor; import java.security.AccessControlException; import java.util.ArrayList; @@ -182,7 +183,7 @@ public class SpringApplication { private Class mainApplicationClass; - private Banner.Mode showBanner = Banner.Mode.CONSOLE; + private Banner.Mode bannerMode = Banner.Mode.CONSOLE; private boolean logStartupInfo = true; @@ -324,7 +325,7 @@ public class SpringApplication { environment = convertToStandardEnvironment(environment); } - if (this.showBanner != Banner.Mode.OFF) { + if (this.bannerMode != Banner.Mode.OFF) { printBanner(environment); } @@ -524,21 +525,23 @@ public class SpringApplication { * banner.location=classpath:banner.txt, banner.charset=UTF-8. If the banner file does * not exist or cannot be printed, a simple default is created. * @param environment the environment - * @see #setShowBanner(org.springframework.boot.Banner.Mode) + * @see #setBannerMode */ protected void printBanner(Environment environment) { Banner selectedBanner = selectBanner(environment); - if (this.showBanner == Banner.Mode.LOG) { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - selectedBanner.printBanner(environment, this.mainApplicationClass, - new PrintStream(baos)); - this.log.info(baos.toString()); + if (this.bannerMode == Banner.Mode.LOG) { + try { + this.log.info(createStringFromBanner(selectedBanner, environment)); + } + catch (UnsupportedEncodingException ex) { + this.log.warn("Failed to create String for banner", ex); + } } else { - selectedBanner - .printBanner(environment, this.mainApplicationClass, System.out); + selectedBanner.printBanner(environment, this.mainApplicationClass, + System.out); } } @@ -557,6 +560,14 @@ public class SpringApplication { return DEFAULT_BANNER; } + private String createStringFromBanner(Banner banner, Environment environment) + throws UnsupportedEncodingException { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + banner.printBanner(environment, this.mainApplicationClass, new PrintStream(baos)); + String charset = environment.getProperty("banner.charset", "UTF-8"); + return baos.toString(charset); + } + /** * Strategy method used to create the {@link ApplicationContext}. By default this * method will respect any explicitly set application context or application context @@ -853,11 +864,22 @@ public class SpringApplication { /** * Sets if the Spring banner should be displayed when the application runs. Defaults - * to {@code org.springframework.boot.Banner.Mode.CONSOLE}. - * @param bannerMode if the banner should be shown in log or console, or turned off. + * to {@code true}. + * @param showBanner if the banner should be shown + * @deprecated since 1.3.0 in favor of {@link #setBannerMode} + */ + @Deprecated + public void setShowBanner(boolean showBanner) { + setBannerMode(showBanner ? Banner.Mode.CONSOLE : Banner.Mode.OFF); + } + + /** + * Sets the mode used to display the banner when the application runs. Defaults to + * {@code Banner.Mode.CONSOLE}. + * @param bannerMode the mode used to display the banner */ - public void setShowBanner(Banner.Mode bannerMode) { - this.showBanner = bannerMode; + public void setBannerMode(Banner.Mode bannerMode) { + this.bannerMode = bannerMode; } /** diff --git a/spring-boot/src/main/java/org/springframework/boot/builder/SpringApplicationBuilder.java b/spring-boot/src/main/java/org/springframework/boot/builder/SpringApplicationBuilder.java index 420ee1b0057..005eda5d3cc 100644 --- a/spring-boot/src/main/java/org/springframework/boot/builder/SpringApplicationBuilder.java +++ b/spring-boot/src/main/java/org/springframework/boot/builder/SpringApplicationBuilder.java @@ -172,7 +172,7 @@ public class SpringApplicationBuilder { web(false); // Probably not interested in multiple banners - showBanner(Banner.Mode.OFF); + bannerMode(Banner.Mode.OFF); // Make sure sources get copied over this.application.setSources(this.sources); @@ -310,12 +310,19 @@ public class SpringApplicationBuilder { * Flag to indicate the startup banner should be printed. * @param showBanner the flag to set. Default true. * @return the current builder + * @deprecated Since 1.3.0 in favor of {@link #bannerMode} */ - public SpringApplicationBuilder showBanner(Banner.Mode showBanner) { + @Deprecated + public SpringApplicationBuilder showBanner(boolean showBanner) { this.application.setShowBanner(showBanner); return this; } + public SpringApplicationBuilder bannerMode(Banner.Mode bannerMode) { + this.application.setBannerMode(bannerMode); + return this; + } + /** * Sets if the application is headless and should not instantiate AWT. Defaults to * {@code true} to prevent java icons appearing. 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 40ac559125b..d446a04abaa 100644 --- a/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java @@ -163,19 +163,37 @@ public class SpringApplicationTests { } @Test - public void disableBanner() throws Exception { + public void disableBannerWithMode() throws Exception { SpringApplication application = spy(new SpringApplication(ExampleConfig.class)); application.setWebEnvironment(false); - application.setShowBanner(Banner.Mode.OFF); + application.setBannerMode(Banner.Mode.OFF); this.context = application.run(); verify(application, never()).printBanner((Environment) anyObject()); } + @SuppressWarnings("deprecation") @Test - public void disableBannerViaProperty() throws Exception { + public void disableBannerWithBoolean() throws Exception { SpringApplication application = spy(new SpringApplication(ExampleConfig.class)); application.setWebEnvironment(false); - this.context = application.run("--spring.main.show_banner=OFF"); + application.setShowBanner(false); + this.context = application.run(); + verify(application, never()).printBanner((Environment) anyObject()); + } + + @Test + public void disableBannerViaShowBannerProperty() throws Exception { + SpringApplication application = spy(new SpringApplication(ExampleConfig.class)); + application.setWebEnvironment(false); + this.context = application.run("--spring.main.show_banner=false"); + verify(application, never()).printBanner((Environment) anyObject()); + } + + @Test + public void disableBannerViaBannerModeProperty() throws Exception { + SpringApplication application = spy(new SpringApplication(ExampleConfig.class)); + application.setWebEnvironment(false); + this.context = application.run("--spring.main.banner-mode=off"); verify(application, never()).printBanner((Environment) anyObject()); } @@ -219,17 +237,8 @@ public class SpringApplicationTests { public void enableBannerInLogViaProperty() throws Exception { SpringApplication application = spy(new SpringApplication(ExampleConfig.class)); application.setWebEnvironment(false); - this.context = application.run("--spring.main.show_banner=LOG"); - verify(application, atLeastOnce()).setShowBanner(Banner.Mode.LOG); - } - - @Test - public void verifyBannerOutputContainsLogInfo() throws Exception { - SpringApplication application = spy(new SpringApplication(ExampleConfig.class)); - application.setWebEnvironment(false); - application.run("--spring.main.show_banner=LOG", - "--banner.location=classpath:test-banner.txt"); - verify(application, atLeastOnce()).setShowBanner(Banner.Mode.LOG); + this.context = application.run("--spring.main.banner-mode=log"); + verify(application, atLeastOnce()).setBannerMode(Banner.Mode.LOG); assertThat(this.output.toString(), containsString("o.s.boot.SpringApplication")); } @@ -565,8 +574,8 @@ public class SpringApplicationTests { TestSpringApplication application = new TestSpringApplication( ExampleConfig.class); application.setWebEnvironment(false); - this.context = application.run("--spring.main.show_banner=OFF"); - assertThat(application.getShowBanner(), is(Banner.Mode.OFF)); + this.context = application.run("--spring.main.banner-mode=OFF"); + assertThat(application.getBannerMode(), is(Banner.Mode.OFF)); } @Test @@ -733,7 +742,7 @@ public class SpringApplicationTests { private boolean useMockLoader; - private Banner.Mode showBanner; + private Banner.Mode bannerMode; TestSpringApplication(Object... sources) { super(sources); @@ -764,13 +773,13 @@ public class SpringApplicationTests { } @Override - public void setShowBanner(Banner.Mode bannerMode) { - super.setShowBanner(bannerMode); - this.showBanner = bannerMode; + public void setBannerMode(Banner.Mode bannerMode) { + super.setBannerMode(bannerMode); + this.bannerMode = bannerMode; } - public Banner.Mode getShowBanner() { - return this.showBanner; + public Banner.Mode getBannerMode() { + return this.bannerMode; } } diff --git a/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileEnvironmentPostProcessorTests.java b/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileEnvironmentPostProcessorTests.java index a8c7fa003da..c9ae2bd971c 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileEnvironmentPostProcessorTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileEnvironmentPostProcessorTests.java @@ -88,7 +88,7 @@ public class ConfigFileEnvironmentPostProcessorTests { } System.clearProperty("the.property"); System.clearProperty("spring.config.location"); - System.clearProperty("spring.main.showBanner"); + System.clearProperty("spring.main.banner-mode"); } @Test @@ -624,7 +624,7 @@ public class ConfigFileEnvironmentPostProcessorTests { // gh-346 this.initializer.setSearchNames("bindtoapplication"); this.initializer.postProcessEnvironment(this.environment, this.application); - Field field = ReflectionUtils.findField(SpringApplication.class, "showBanner"); + Field field = ReflectionUtils.findField(SpringApplication.class, "bannerMode"); field.setAccessible(true); assertThat((Banner.Mode) field.get(this.application), equalTo(Banner.Mode.OFF)); } @@ -632,9 +632,9 @@ public class ConfigFileEnvironmentPostProcessorTests { @Test public void bindsSystemPropertyToSpringApplication() throws Exception { // gh-951 - System.setProperty("spring.main.showBanner", "OFF"); + System.setProperty("spring.main.banner-mode", "off"); this.initializer.postProcessEnvironment(this.environment, this.application); - Field field = ReflectionUtils.findField(SpringApplication.class, "showBanner"); + Field field = ReflectionUtils.findField(SpringApplication.class, "bannerMode"); field.setAccessible(true); assertThat((Banner.Mode) field.get(this.application), equalTo(Banner.Mode.OFF)); } diff --git a/spring-boot/src/test/resources/bindtoapplication.properties b/spring-boot/src/test/resources/bindtoapplication.properties index ec0094f3fda..33406d213f8 100644 --- a/spring-boot/src/test/resources/bindtoapplication.properties +++ b/spring-boot/src/test/resources/bindtoapplication.properties @@ -1 +1 @@ -spring.main.show_banner=OFF +spring.main.banner-mode=off