Skip to content

Commit 03b43c6

Browse files
committed
Change the printer so it no longer holds the Banner as state
1 parent 66190ef commit 03b43c6

File tree

5 files changed

+55
-75
lines changed

5 files changed

+55
-75
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/DefaultSpringApplicationBannerPrinter.java

Lines changed: 13 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -17,94 +17,43 @@
1717
package org.springframework.boot;
1818

1919
import java.io.ByteArrayOutputStream;
20-
import java.io.IOException;
2120
import java.io.PrintStream;
2221
import java.io.UnsupportedEncodingException;
2322
import java.nio.charset.StandardCharsets;
2423

2524
import org.apache.commons.logging.Log;
2625
import org.apache.commons.logging.LogFactory;
2726

28-
import org.springframework.boot.Banner.Mode;
2927
import org.springframework.core.env.Environment;
30-
import org.springframework.core.io.Resource;
31-
import org.springframework.core.io.ResourceLoader;
3228

3329
/**
3430
* Class used by {@link SpringApplication} to print the application banner.
3531
*
3632
* @author Phillip Webb
33+
* @author Junhyung Park
3734
*/
3835
class DefaultSpringApplicationBannerPrinter implements SpringApplicationBannerPrinter {
3936

4037
private static final Log logger = LogFactory.getLog(DefaultSpringApplicationBannerPrinter.class);
4138

42-
private static final Banner DEFAULT_BANNER = new SpringBootBanner();
43-
44-
private final ResourceLoader resourceLoader;
45-
46-
private final Banner fallbackBanner;
47-
48-
DefaultSpringApplicationBannerPrinter(ResourceLoader resourceLoader, Banner fallbackBanner) {
49-
this.resourceLoader = resourceLoader;
50-
this.fallbackBanner = fallbackBanner;
51-
}
52-
5339
@Override
54-
public Banner print(Environment environment, Class<?> sourceClass, Mode bannerMode) {
40+
public Banner print(Environment environment, Class<?> sourceClass, Banner.Mode bannerMode, Banner banner) {
41+
banner.printBanner(environment, sourceClass, System.out);
5542
switch (bannerMode) {
56-
case CONSOLE -> {
57-
return print(environment, sourceClass, System.out);
58-
}
59-
case LOG -> {
60-
return print(environment, sourceClass, logger);
61-
}
43+
case OFF:
44+
case LOG:
45+
try {
46+
logger.info(createStringFromBanner(banner, environment, sourceClass));
47+
}
48+
catch (UnsupportedEncodingException ex) {
49+
logger.warn("Failed to create String for banner", ex);
50+
}
51+
case CONSOLE:
52+
banner.printBanner(environment, sourceClass, System.out);
6253
}
63-
return new PrintedBanner(getBanner(environment), sourceClass);
64-
}
65-
66-
private Banner print(Environment environment, Class<?> sourceClass, Log logger) {
67-
Banner banner = getBanner(environment);
68-
try {
69-
logger.info(createStringFromBanner(banner, environment, sourceClass));
70-
}
71-
catch (UnsupportedEncodingException ex) {
72-
logger.warn("Failed to create String for banner", ex);
73-
}
74-
return new PrintedBanner(banner, sourceClass);
75-
}
76-
77-
private Banner print(Environment environment, Class<?> sourceClass, PrintStream out) {
78-
Banner banner = getBanner(environment);
79-
banner.printBanner(environment, sourceClass, out);
8054
return new PrintedBanner(banner, sourceClass);
8155
}
8256

83-
private Banner getBanner(Environment environment) {
84-
Banner textBanner = getTextBanner(environment);
85-
if (textBanner != null) {
86-
return textBanner;
87-
}
88-
if (this.fallbackBanner != null) {
89-
return this.fallbackBanner;
90-
}
91-
return DEFAULT_BANNER;
92-
}
93-
94-
private Banner getTextBanner(Environment environment) {
95-
String location = environment.getProperty(BANNER_LOCATION_PROPERTY, DEFAULT_BANNER_LOCATION);
96-
Resource resource = this.resourceLoader.getResource(location);
97-
try {
98-
if (resource.exists() && !resource.getURL().toExternalForm().contains("liquibase-core")) {
99-
return new ResourceBanner(resource);
100-
}
101-
}
102-
catch (IOException ex) {
103-
// Ignore
104-
}
105-
return null;
106-
}
107-
10857
private String createStringFromBanner(Banner banner, Environment environment, Class<?> mainApplicationClass)
10958
throws UnsupportedEncodingException {
11059
String charset = environment.getProperty("spring.banner.charset", StandardCharsets.UTF_8.name());

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.boot;
1818

19+
import java.io.IOException;
1920
import java.lang.StackWalker.StackFrame;
2021
import java.lang.management.ManagementFactory;
2122
import java.lang.reflect.Method;
@@ -56,7 +57,6 @@
5657
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
5758
import org.springframework.beans.factory.support.RootBeanDefinition;
5859
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
59-
import org.springframework.boot.Banner.Mode;
6060
import org.springframework.boot.context.properties.bind.Bindable;
6161
import org.springframework.boot.context.properties.bind.Binder;
6262
import org.springframework.boot.context.properties.source.ConfigurationPropertySources;
@@ -93,6 +93,7 @@
9393
import org.springframework.core.env.PropertySource;
9494
import org.springframework.core.env.SimpleCommandLinePropertySource;
9595
import org.springframework.core.io.DefaultResourceLoader;
96+
import org.springframework.core.io.Resource;
9697
import org.springframework.core.io.ResourceLoader;
9798
import org.springframework.core.io.support.SpringFactoriesLoader;
9899
import org.springframework.core.io.support.SpringFactoriesLoader.ArgumentResolver;
@@ -560,12 +561,39 @@ private Banner printBanner(ConfigurableEnvironment environment) {
560561
if (this.properties.getBannerMode(environment) == Banner.Mode.OFF) {
561562
return null;
562563
}
563-
ResourceLoader resourceLoader = (this.resourceLoader != null) ? this.resourceLoader
564-
: new DefaultResourceLoader(null);
565-
566564
SpringApplicationBannerPrinter bannerPrinter = Objects.requireNonNullElseGet(this.bannerPrinter,
567-
() -> new DefaultSpringApplicationBannerPrinter(resourceLoader, this.banner));
568-
return bannerPrinter.print(environment, this.mainApplicationClass, this.properties.getBannerMode(environment));
565+
DefaultSpringApplicationBannerPrinter::new);
566+
Banner banner = this.banner;
567+
if (banner == null) {
568+
banner = getFallbackBanner(environment);
569+
}
570+
return bannerPrinter.print(environment, this.mainApplicationClass, this.properties.getBannerMode(environment),
571+
banner);
572+
}
573+
574+
private Banner getFallbackBanner(Environment environment) {
575+
Banner textBanner = getTextBanner(environment);
576+
if (textBanner != null) {
577+
return textBanner;
578+
}
579+
return new SpringBootBanner();
580+
}
581+
582+
private Banner getTextBanner(Environment environment) {
583+
if (this.resourceLoader == null) {
584+
return null;
585+
}
586+
String location = environment.getProperty(BANNER_LOCATION_PROPERTY, BANNER_LOCATION_PROPERTY_VALUE);
587+
Resource resource = this.resourceLoader.getResource(location);
588+
try {
589+
if (resource.exists() && !resource.getURL().toExternalForm().contains("liquibase-core")) {
590+
return new ResourceBanner(resource);
591+
}
592+
}
593+
catch (IOException ex) {
594+
// Ignore
595+
}
596+
return null;
569597
}
570598

571599
/**

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplicationBannerPrinter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public interface SpringApplicationBannerPrinter {
3333

3434
String DEFAULT_BANNER_LOCATION = "banner.txt";
3535

36-
Banner print(Environment environment, Class<?> sourceClass, Banner.Mode bannerMode);
36+
Banner print(Environment environment, Class<?> sourceClass, Banner.Mode mode, Banner banner);
3737

3838
class SpringApplicationBannerPrinterRuntimeHints implements RuntimeHintsRegistrar {
3939

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/DefaultSpringApplicationBannerPrinterTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ class DefaultSpringApplicationBannerPrinterTests {
4242
void shouldUseUtf8(CapturedOutput capturedOutput) {
4343
ResourceLoader resourceLoader = new GenericApplicationContext();
4444
Resource resource = resourceLoader.getResource("classpath:/banner-utf8.txt");
45-
SpringApplicationBannerPrinter printer = new DefaultSpringApplicationBannerPrinter(resourceLoader,
45+
SpringApplicationBannerPrinter printer = new DefaultSpringApplicationBannerPrinter();
46+
printer.print(new MockEnvironment(), DefaultSpringApplicationBannerPrinterTests.class, Mode.LOG,
4647
new ResourceBanner(resource));
47-
printer.print(new MockEnvironment(), DefaultSpringApplicationBannerPrinterTests.class, Mode.LOG);
4848
assertThat(capturedOutput).containsIgnoringNewLines("\uD83D\uDE0D Spring Boot! \uD83D\uDE0D");
4949
}
5050

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/SpringApplicationBannerPrinterTests.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,18 @@ void shouldPrintWithDefaultPrinter(CapturedOutput capturedOutput) {
7777
private SpringApplication createSpringApplicationWithBannerModeLog() {
7878
SpringApplication application = new SpringApplication(Config.class);
7979
application.setBannerMode(Mode.LOG);
80+
application.setBanner(new DummyBanner());
8081
application.setWebApplicationType(WebApplicationType.NONE);
8182
return application;
8283
}
8384

8485
static class CustomSpringApplicationBannerPrinter implements SpringApplicationBannerPrinter {
8586

8687
@Override
87-
public Banner print(Environment environment, Class<?> sourceClass, Mode bannerMode) {
88-
Banner banner = new DummyBanner();
88+
public Banner print(Environment environment, Class<?> sourceClass, Mode bannerMode, Banner banner) {
89+
if (banner == null) {
90+
return null;
91+
}
8992
banner.printBanner(environment, sourceClass, System.out);
9093
return banner;
9194
}

0 commit comments

Comments
 (0)