Merge pull request #5636 from michael-simons/add_banner_bean

* add_banner_bean:
  Register printed banner in ApplicationContext
pull/5662/head
Phillip Webb 9 years ago
commit caf9bd9be2

@ -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 to be printed on `System.out` (`console`), using the configured logger (`log`) or not
at all (`off`). at all (`off`).
The printed banner will be registered as a singleton bean under the name
`springBootBanner`.
[NOTE] [NOTE]
==== ====
YAML maps `off` to `false` so make sure to add quotes if you want to disable the YAML maps `off` to `false` so make sure to add quotes if you want to disable the

@ -139,6 +139,7 @@ import org.springframework.web.context.support.StandardServletEnvironment;
* @author Stephane Nicoll * @author Stephane Nicoll
* @author Jeremy Rickard * @author Jeremy Rickard
* @author Craig Burke * @author Craig Burke
* @author Michael Simons
* @see #run(Object, String[]) * @see #run(Object, String[])
* @see #run(Object[], String[]) * @see #run(Object[], String[])
* @see #SpringApplication(Object...) * @see #SpringApplication(Object...)
@ -200,6 +201,8 @@ public class SpringApplication {
private Banner banner; private Banner banner;
private boolean printedCustomBannerViaDeprecatedMethod;
private ResourceLoader resourceLoader; private ResourceLoader resourceLoader;
private BeanNameGenerator beanNameGenerator; private BeanNameGenerator beanNameGenerator;
@ -304,11 +307,10 @@ public class SpringApplication {
args); args);
ConfigurableEnvironment environment = prepareEnvironment(listeners, ConfigurableEnvironment environment = prepareEnvironment(listeners,
applicationArguments); applicationArguments);
if (this.bannerMode != Banner.Mode.OFF) { Banner printedBanner = printBanner(environment);
printBanner(environment);
}
context = createApplicationContext(); context = createApplicationContext();
prepareContext(context, environment, listeners, applicationArguments); prepareContext(context, environment, listeners, applicationArguments,
printedBanner);
refreshContext(context); refreshContext(context);
afterRefresh(context, applicationArguments); afterRefresh(context, applicationArguments);
listeners.finished(context, null); listeners.finished(context, null);
@ -340,7 +342,7 @@ public class SpringApplication {
private void prepareContext(ConfigurableApplicationContext context, private void prepareContext(ConfigurableApplicationContext context,
ConfigurableEnvironment environment, SpringApplicationRunListeners listeners, ConfigurableEnvironment environment, SpringApplicationRunListeners listeners,
ApplicationArguments applicationArguments) { ApplicationArguments applicationArguments, Banner printedBanner) {
context.setEnvironment(environment); context.setEnvironment(environment);
postProcessApplicationContext(context); postProcessApplicationContext(context);
applyInitializers(context); applyInitializers(context);
@ -353,6 +355,9 @@ public class SpringApplication {
// Add boot specific singleton beans // Add boot specific singleton beans
context.getBeanFactory().registerSingleton("springApplicationArguments", context.getBeanFactory().registerSingleton("springApplicationArguments",
applicationArguments); applicationArguments);
if (printedBanner != null) {
context.getBeanFactory().registerSingleton("springBootBanner", printedBanner);
}
// Load the sources // Load the sources
Set<Object> sources = getSources(); Set<Object> sources = getSources();
@ -530,6 +535,29 @@ public class SpringApplication {
environment.setActiveProfiles(profiles.toArray(new String[profiles.size()])); 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 * 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 * 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. * not exist or cannot be printed, a simple default is created.
* @param environment the environment * @param environment the environment
* @see #setBannerMode * @see #setBannerMode
* @deprecated as of 1.4 in favor of @{@link #setBanner(Banner)}
*/ */
@Deprecated
protected void printBanner(Environment environment) { protected void printBanner(Environment environment) {
ResourceLoader resourceLoader = this.resourceLoader != null ? this.resourceLoader this.printedCustomBannerViaDeprecatedMethod = false;
: 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);
}
} }
/** /**

@ -55,7 +55,7 @@ class SpringApplicationBannerPrinter {
this.fallbackBanner = fallbackBanner; 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); Banner banner = getBanner(environment, this.fallbackBanner);
try { try {
logger.info(createStringFromBanner(banner, environment, sourceClass)); logger.info(createStringFromBanner(banner, environment, sourceClass));
@ -63,11 +63,13 @@ class SpringApplicationBannerPrinter {
catch (UnsupportedEncodingException ex) { catch (UnsupportedEncodingException ex) {
logger.warn("Failed to create String for banner", 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 banner = getBanner(environment, this.fallbackBanner);
banner.printBanner(environment, sourceClass, out); banner.printBanner(environment, sourceClass, out);
return banner;
} }
private Banner getBanner(Environment environment, Banner definedBanner) { private Banner getBanner(Environment environment, Banner definedBanner) {

@ -22,6 +22,7 @@ import org.junit.After;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.Banner.Mode;
import org.springframework.boot.testutil.InternalOutputCapture; import org.springframework.boot.testutil.InternalOutputCapture;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@ -34,6 +35,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* *
* @author Phillip Webb * @author Phillip Webb
* @author Michael Stummvoll * @author Michael Stummvoll
* @author Michael Simons
*/ */
public class BannerTests { public class BannerTests {
@ -74,6 +76,49 @@ public class BannerTests {
assertThat(this.out.toString()).contains("My Banner"); 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 { static class DummyBanner implements Banner {
@Override @Override

Loading…
Cancel
Save