From aaed87d1763fe07b233540081ee69dfdce723377 Mon Sep 17 00:00:00 2001 From: "Michael J. Simons" Date: Mon, 11 Apr 2016 17:50:24 +0100 Subject: [PATCH] Register printed banner in ApplicationContext Update SpringApplication to store the banner that was actually printed as a bean named `springBootBanner`. Closes gh-5636 --- .../main/asciidoc/spring-boot-features.adoc | 3 ++ .../boot/SpringApplication.java | 51 +++++++++++++------ .../boot/SpringApplicationBannerPrinter.java | 6 ++- .../org/springframework/boot/BannerTests.java | 45 ++++++++++++++++ 4 files changed, 88 insertions(+), 17 deletions(-) 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 1a1b7dd1a3..bd92cf3553 100644 --- a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc +++ b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc @@ -94,6 +94,9 @@ You can also use the `spring.main.banner-mode` property to determine if the bann to be printed on `System.out` (`console`), using the configured logger (`log`) or not at all (`off`). +The printed banner will be registered as a singleton bean under the name +`springBootBanner`. + [NOTE] ==== YAML maps `off` to `false` so make sure to add quotes if you want to disable the 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 3d665a5552..fa52ee54b0 100644 --- a/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java +++ b/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java @@ -139,6 +139,7 @@ import org.springframework.web.context.support.StandardServletEnvironment; * @author Stephane Nicoll * @author Jeremy Rickard * @author Craig Burke + * @author Michael Simons * @see #run(Object, String[]) * @see #run(Object[], String[]) * @see #SpringApplication(Object...) @@ -200,6 +201,8 @@ public class SpringApplication { private Banner banner; + private boolean printedCustomBannerViaDeprecatedMethod; + private ResourceLoader resourceLoader; private BeanNameGenerator beanNameGenerator; @@ -304,11 +307,10 @@ public class SpringApplication { args); ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments); - if (this.bannerMode != Banner.Mode.OFF) { - printBanner(environment); - } + Banner printedBanner = printBanner(environment); context = createApplicationContext(); - prepareContext(context, environment, listeners, applicationArguments); + prepareContext(context, environment, listeners, applicationArguments, + printedBanner); refreshContext(context); afterRefresh(context, applicationArguments); listeners.finished(context, null); @@ -340,7 +342,7 @@ public class SpringApplication { private void prepareContext(ConfigurableApplicationContext context, ConfigurableEnvironment environment, SpringApplicationRunListeners listeners, - ApplicationArguments applicationArguments) { + ApplicationArguments applicationArguments, Banner printedBanner) { context.setEnvironment(environment); postProcessApplicationContext(context); applyInitializers(context); @@ -353,6 +355,9 @@ public class SpringApplication { // Add boot specific singleton beans context.getBeanFactory().registerSingleton("springApplicationArguments", applicationArguments); + if (printedBanner != null) { + context.getBeanFactory().registerSingleton("springBootBanner", printedBanner); + } // Load the sources Set sources = getSources(); @@ -530,6 +535,29 @@ public class SpringApplication { environment.setActiveProfiles(profiles.toArray(new String[profiles.size()])); } + private Banner printBanner(ConfigurableEnvironment environment) { + if (printBannerViaDeprecatedMethod(environment)) { + return null; + } + if (this.bannerMode == Banner.Mode.OFF) { + return null; + } + ResourceLoader resourceLoader = this.resourceLoader != null ? this.resourceLoader + : new DefaultResourceLoader(getClassLoader()); + SpringApplicationBannerPrinter bannerPrinter = new SpringApplicationBannerPrinter( + resourceLoader, this.banner); + if (this.bannerMode == Mode.LOG) { + return bannerPrinter.print(environment, this.mainApplicationClass, logger); + } + return bannerPrinter.print(environment, this.mainApplicationClass, System.out); + } + + private boolean printBannerViaDeprecatedMethod(Environment environment) { + this.printedCustomBannerViaDeprecatedMethod = true; + printBanner(environment); + return this.printedCustomBannerViaDeprecatedMethod; + } + /** * 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 @@ -537,18 +565,11 @@ public class SpringApplication { * not exist or cannot be printed, a simple default is created. * @param environment the environment * @see #setBannerMode + * @deprecated as of 1.4 in favor of @{@link #setBanner(Banner)} */ + @Deprecated protected void printBanner(Environment environment) { - ResourceLoader resourceLoader = this.resourceLoader != null ? this.resourceLoader - : new DefaultResourceLoader(getClassLoader()); - SpringApplicationBannerPrinter banner = new SpringApplicationBannerPrinter(resourceLoader, - this.banner); - if (this.bannerMode == Mode.LOG) { - banner.print(environment, this.mainApplicationClass, logger); - } - else { - banner.print(environment, this.mainApplicationClass, System.out); - } + this.printedCustomBannerViaDeprecatedMethod = false; } /** diff --git a/spring-boot/src/main/java/org/springframework/boot/SpringApplicationBannerPrinter.java b/spring-boot/src/main/java/org/springframework/boot/SpringApplicationBannerPrinter.java index b3241b6968..d702031430 100644 --- a/spring-boot/src/main/java/org/springframework/boot/SpringApplicationBannerPrinter.java +++ b/spring-boot/src/main/java/org/springframework/boot/SpringApplicationBannerPrinter.java @@ -55,7 +55,7 @@ class SpringApplicationBannerPrinter { this.fallbackBanner = fallbackBanner; } - public void print(Environment environment, Class sourceClass, Log logger) { + public Banner print(Environment environment, Class sourceClass, Log logger) { Banner banner = getBanner(environment, this.fallbackBanner); try { logger.info(createStringFromBanner(banner, environment, sourceClass)); @@ -63,11 +63,13 @@ class SpringApplicationBannerPrinter { catch (UnsupportedEncodingException ex) { logger.warn("Failed to create String for banner", ex); } + return banner; } - public void print(Environment environment, Class sourceClass, PrintStream out) { + public Banner print(Environment environment, Class sourceClass, PrintStream out) { Banner banner = getBanner(environment, this.fallbackBanner); banner.printBanner(environment, sourceClass, out); + return banner; } private Banner getBanner(Environment environment, Banner definedBanner) { 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 140a6ea74b..bea69bc190 100644 --- a/spring-boot/src/test/java/org/springframework/boot/BannerTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/BannerTests.java @@ -22,6 +22,7 @@ import org.junit.After; import org.junit.Rule; import org.junit.Test; +import org.springframework.boot.Banner.Mode; import org.springframework.boot.testutil.InternalOutputCapture; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.Configuration; @@ -34,6 +35,7 @@ import static org.assertj.core.api.Assertions.assertThat; * * @author Phillip Webb * @author Michael Stummvoll + * @author Michael Simons */ public class BannerTests { @@ -74,6 +76,49 @@ public class BannerTests { assertThat(this.out.toString()).contains("My Banner"); } + @Test + public void testBannerInContext() throws Exception { + SpringApplication application = new SpringApplication(Config.class); + application.setWebEnvironment(false); + this.context = application.run(); + assertThat(this.context.containsBean("springBootBanner")).isTrue(); + } + + @Test + public void testCustomBannerInContext() throws Exception { + SpringApplication application = new SpringApplication(Config.class); + application.setWebEnvironment(false); + final DummyBanner dummyBanner = new DummyBanner(); + application.setBanner(dummyBanner); + this.context = application.run(); + assertThat(this.context.getBean("springBootBanner")).isEqualTo(dummyBanner); + } + + @Test + public void testDisableBannerInContext() throws Exception { + SpringApplication application = new SpringApplication(Config.class); + application.setBannerMode(Mode.OFF); + application.setWebEnvironment(false); + this.context = application.run(); + assertThat(this.context.containsBean("springBootBanner")).isFalse(); + } + + @Test + public void testDeprecatePrintBanner() throws Exception { + SpringApplication application = new SpringApplication(Config.class) { + + @Override + protected void printBanner(Environment environment) { + System.out.println("I printed a deprecated banner"); + }; + + }; + application.setWebEnvironment(false); + this.context = application.run(); + assertThat(this.out.toString()).contains("I printed a deprecated banner"); + assertThat(this.context.containsBean("springBootBanner")).isFalse(); + } + static class DummyBanner implements Banner { @Override