Register printed banner in ApplicationContext
Update SpringApplication to store the banner that was actually printed as a bean named `springBootBanner`. Closes gh-5636
This commit is contained in:
		
							parent
							
								
									e14329c77b
								
							
						
					
					
						commit
						aaed87d176
					
				| 
						 | 
				
			
			@ -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
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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<Object> 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;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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) {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue