Rework breaking API changes
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
This commit is contained in:
parent
3090659971
commit
01eb4cf954
|
|
@ -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.sources= # sources (class name, package name or XML resource location) to include
|
||||||
spring.main.web-environment= # detect by default
|
spring.main.web-environment= # detect by default
|
||||||
spring.main.show-banner=true
|
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
|
spring.main....= # see class for all properties
|
||||||
|
|
||||||
# AUTO-CONFIGURATION
|
# AUTO-CONFIGURATION
|
||||||
|
|
|
||||||
|
|
@ -123,7 +123,7 @@ might have.
|
||||||
[source,properties,indent=0,subs="verbatim,quotes,attributes"]
|
[source,properties,indent=0,subs="verbatim,quotes,attributes"]
|
||||||
----
|
----
|
||||||
spring.main.web_environment=false
|
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
|
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]
|
[source,java,indent=0]
|
||||||
----
|
----
|
||||||
new SpringApplicationBuilder()
|
new SpringApplicationBuilder()
|
||||||
.showBanner(Banner.Mode.OFF)
|
.bannerMode(Banner.Mode.OFF)
|
||||||
.sources(demo.MyApp.class)
|
.sources(demo.MyApp.class)
|
||||||
.run(args);
|
.run(args);
|
||||||
----
|
----
|
||||||
|
|
@ -149,7 +149,7 @@ used with the following configuration:
|
||||||
[source,properties,indent=0,subs="verbatim,quotes,attributes"]
|
[source,properties,indent=0,subs="verbatim,quotes,attributes"]
|
||||||
----
|
----
|
||||||
spring.main.sources=com.acme.Config,com.acme.ExtraConfig
|
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
|
The actual application will _now_ show the banner (as overridden by configuration) and use
|
||||||
|
|
|
||||||
|
|
@ -93,7 +93,7 @@ instance and customize it. For example, to turn off the banner you would write:
|
||||||
----
|
----
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SpringApplication app = new SpringApplication(MySpringConfiguration.class);
|
SpringApplication app = new SpringApplication(MySpringConfiguration.class);
|
||||||
app.setShowBanner(false);
|
app.setBannerMode(Banner.Mode.OFF);
|
||||||
app.run(args);
|
app.run(args);
|
||||||
}
|
}
|
||||||
----
|
----
|
||||||
|
|
@ -124,7 +124,7 @@ For example:
|
||||||
[source,java,indent=0]
|
[source,java,indent=0]
|
||||||
----
|
----
|
||||||
new SpringApplicationBuilder()
|
new SpringApplicationBuilder()
|
||||||
.showBanner(Banner.Mode.OFF)
|
.bannerMode(Banner.Mode.OFF)
|
||||||
.sources(Parent.class)
|
.sources(Parent.class)
|
||||||
.child(Application.class)
|
.child(Application.class)
|
||||||
.run(args);
|
.run(args);
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,7 @@ package org.springframework.boot;
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.PrintStream;
|
import java.io.PrintStream;
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.lang.reflect.Constructor;
|
import java.lang.reflect.Constructor;
|
||||||
import java.security.AccessControlException;
|
import java.security.AccessControlException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
@ -182,7 +183,7 @@ public class SpringApplication {
|
||||||
|
|
||||||
private Class<?> mainApplicationClass;
|
private Class<?> mainApplicationClass;
|
||||||
|
|
||||||
private Banner.Mode showBanner = Banner.Mode.CONSOLE;
|
private Banner.Mode bannerMode = Banner.Mode.CONSOLE;
|
||||||
|
|
||||||
private boolean logStartupInfo = true;
|
private boolean logStartupInfo = true;
|
||||||
|
|
||||||
|
|
@ -324,7 +325,7 @@ public class SpringApplication {
|
||||||
environment = convertToStandardEnvironment(environment);
|
environment = convertToStandardEnvironment(environment);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.showBanner != Banner.Mode.OFF) {
|
if (this.bannerMode != Banner.Mode.OFF) {
|
||||||
printBanner(environment);
|
printBanner(environment);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -524,21 +525,23 @@ public class SpringApplication {
|
||||||
* banner.location=classpath:banner.txt, banner.charset=UTF-8. If the banner file does
|
* 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.
|
* not exist or cannot be printed, a simple default is created.
|
||||||
* @param environment the environment
|
* @param environment the environment
|
||||||
* @see #setShowBanner(org.springframework.boot.Banner.Mode)
|
* @see #setBannerMode
|
||||||
*/
|
*/
|
||||||
protected void printBanner(Environment environment) {
|
protected void printBanner(Environment environment) {
|
||||||
|
|
||||||
Banner selectedBanner = selectBanner(environment);
|
Banner selectedBanner = selectBanner(environment);
|
||||||
|
|
||||||
if (this.showBanner == Banner.Mode.LOG) {
|
if (this.bannerMode == Banner.Mode.LOG) {
|
||||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
try {
|
||||||
selectedBanner.printBanner(environment, this.mainApplicationClass,
|
this.log.info(createStringFromBanner(selectedBanner, environment));
|
||||||
new PrintStream(baos));
|
}
|
||||||
this.log.info(baos.toString());
|
catch (UnsupportedEncodingException ex) {
|
||||||
|
this.log.warn("Failed to create String for banner", ex);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
selectedBanner
|
selectedBanner.printBanner(environment, this.mainApplicationClass,
|
||||||
.printBanner(environment, this.mainApplicationClass, System.out);
|
System.out);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -557,6 +560,14 @@ public class SpringApplication {
|
||||||
return DEFAULT_BANNER;
|
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
|
* Strategy method used to create the {@link ApplicationContext}. By default this
|
||||||
* method will respect any explicitly set application context or application context
|
* 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
|
* Sets if the Spring banner should be displayed when the application runs. Defaults
|
||||||
* to {@code org.springframework.boot.Banner.Mode.CONSOLE}.
|
* to {@code true}.
|
||||||
* @param bannerMode if the banner should be shown in log or console, or turned off.
|
* @param showBanner if the banner should be shown
|
||||||
|
* @deprecated since 1.3.0 in favor of {@link #setBannerMode}
|
||||||
*/
|
*/
|
||||||
public void setShowBanner(Banner.Mode bannerMode) {
|
@Deprecated
|
||||||
this.showBanner = bannerMode;
|
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 setBannerMode(Banner.Mode bannerMode) {
|
||||||
|
this.bannerMode = bannerMode;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -172,7 +172,7 @@ public class SpringApplicationBuilder {
|
||||||
web(false);
|
web(false);
|
||||||
|
|
||||||
// Probably not interested in multiple banners
|
// Probably not interested in multiple banners
|
||||||
showBanner(Banner.Mode.OFF);
|
bannerMode(Banner.Mode.OFF);
|
||||||
|
|
||||||
// Make sure sources get copied over
|
// Make sure sources get copied over
|
||||||
this.application.setSources(this.sources);
|
this.application.setSources(this.sources);
|
||||||
|
|
@ -310,12 +310,19 @@ public class SpringApplicationBuilder {
|
||||||
* Flag to indicate the startup banner should be printed.
|
* Flag to indicate the startup banner should be printed.
|
||||||
* @param showBanner the flag to set. Default true.
|
* @param showBanner the flag to set. Default true.
|
||||||
* @return the current builder
|
* @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);
|
this.application.setShowBanner(showBanner);
|
||||||
return this;
|
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
|
* Sets if the application is headless and should not instantiate AWT. Defaults to
|
||||||
* {@code true} to prevent java icons appearing.
|
* {@code true} to prevent java icons appearing.
|
||||||
|
|
|
||||||
|
|
@ -163,19 +163,37 @@ public class SpringApplicationTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void disableBanner() throws Exception {
|
public void disableBannerWithMode() throws Exception {
|
||||||
SpringApplication application = spy(new SpringApplication(ExampleConfig.class));
|
SpringApplication application = spy(new SpringApplication(ExampleConfig.class));
|
||||||
application.setWebEnvironment(false);
|
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 disableBannerWithBoolean() throws Exception {
|
||||||
|
SpringApplication application = spy(new SpringApplication(ExampleConfig.class));
|
||||||
|
application.setWebEnvironment(false);
|
||||||
|
application.setShowBanner(false);
|
||||||
this.context = application.run();
|
this.context = application.run();
|
||||||
verify(application, never()).printBanner((Environment) anyObject());
|
verify(application, never()).printBanner((Environment) anyObject());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void disableBannerViaProperty() throws Exception {
|
public void disableBannerViaShowBannerProperty() throws Exception {
|
||||||
SpringApplication application = spy(new SpringApplication(ExampleConfig.class));
|
SpringApplication application = spy(new SpringApplication(ExampleConfig.class));
|
||||||
application.setWebEnvironment(false);
|
application.setWebEnvironment(false);
|
||||||
this.context = application.run("--spring.main.show_banner=OFF");
|
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());
|
verify(application, never()).printBanner((Environment) anyObject());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -219,17 +237,8 @@ public class SpringApplicationTests {
|
||||||
public void enableBannerInLogViaProperty() throws Exception {
|
public void enableBannerInLogViaProperty() throws Exception {
|
||||||
SpringApplication application = spy(new SpringApplication(ExampleConfig.class));
|
SpringApplication application = spy(new SpringApplication(ExampleConfig.class));
|
||||||
application.setWebEnvironment(false);
|
application.setWebEnvironment(false);
|
||||||
this.context = application.run("--spring.main.show_banner=LOG");
|
this.context = application.run("--spring.main.banner-mode=log");
|
||||||
verify(application, atLeastOnce()).setShowBanner(Banner.Mode.LOG);
|
verify(application, atLeastOnce()).setBannerMode(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"));
|
assertThat(this.output.toString(), containsString("o.s.boot.SpringApplication"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -565,8 +574,8 @@ public class SpringApplicationTests {
|
||||||
TestSpringApplication application = new TestSpringApplication(
|
TestSpringApplication application = new TestSpringApplication(
|
||||||
ExampleConfig.class);
|
ExampleConfig.class);
|
||||||
application.setWebEnvironment(false);
|
application.setWebEnvironment(false);
|
||||||
this.context = application.run("--spring.main.show_banner=OFF");
|
this.context = application.run("--spring.main.banner-mode=OFF");
|
||||||
assertThat(application.getShowBanner(), is(Banner.Mode.OFF));
|
assertThat(application.getBannerMode(), is(Banner.Mode.OFF));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
@ -733,7 +742,7 @@ public class SpringApplicationTests {
|
||||||
|
|
||||||
private boolean useMockLoader;
|
private boolean useMockLoader;
|
||||||
|
|
||||||
private Banner.Mode showBanner;
|
private Banner.Mode bannerMode;
|
||||||
|
|
||||||
TestSpringApplication(Object... sources) {
|
TestSpringApplication(Object... sources) {
|
||||||
super(sources);
|
super(sources);
|
||||||
|
|
@ -764,13 +773,13 @@ public class SpringApplicationTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setShowBanner(Banner.Mode bannerMode) {
|
public void setBannerMode(Banner.Mode bannerMode) {
|
||||||
super.setShowBanner(bannerMode);
|
super.setBannerMode(bannerMode);
|
||||||
this.showBanner = bannerMode;
|
this.bannerMode = bannerMode;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Banner.Mode getShowBanner() {
|
public Banner.Mode getBannerMode() {
|
||||||
return this.showBanner;
|
return this.bannerMode;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -88,7 +88,7 @@ public class ConfigFileEnvironmentPostProcessorTests {
|
||||||
}
|
}
|
||||||
System.clearProperty("the.property");
|
System.clearProperty("the.property");
|
||||||
System.clearProperty("spring.config.location");
|
System.clearProperty("spring.config.location");
|
||||||
System.clearProperty("spring.main.showBanner");
|
System.clearProperty("spring.main.banner-mode");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
@ -624,7 +624,7 @@ public class ConfigFileEnvironmentPostProcessorTests {
|
||||||
// gh-346
|
// gh-346
|
||||||
this.initializer.setSearchNames("bindtoapplication");
|
this.initializer.setSearchNames("bindtoapplication");
|
||||||
this.initializer.postProcessEnvironment(this.environment, this.application);
|
this.initializer.postProcessEnvironment(this.environment, this.application);
|
||||||
Field field = ReflectionUtils.findField(SpringApplication.class, "showBanner");
|
Field field = ReflectionUtils.findField(SpringApplication.class, "bannerMode");
|
||||||
field.setAccessible(true);
|
field.setAccessible(true);
|
||||||
assertThat((Banner.Mode) field.get(this.application), equalTo(Banner.Mode.OFF));
|
assertThat((Banner.Mode) field.get(this.application), equalTo(Banner.Mode.OFF));
|
||||||
}
|
}
|
||||||
|
|
@ -632,9 +632,9 @@ public class ConfigFileEnvironmentPostProcessorTests {
|
||||||
@Test
|
@Test
|
||||||
public void bindsSystemPropertyToSpringApplication() throws Exception {
|
public void bindsSystemPropertyToSpringApplication() throws Exception {
|
||||||
// gh-951
|
// gh-951
|
||||||
System.setProperty("spring.main.showBanner", "OFF");
|
System.setProperty("spring.main.banner-mode", "off");
|
||||||
this.initializer.postProcessEnvironment(this.environment, this.application);
|
this.initializer.postProcessEnvironment(this.environment, this.application);
|
||||||
Field field = ReflectionUtils.findField(SpringApplication.class, "showBanner");
|
Field field = ReflectionUtils.findField(SpringApplication.class, "bannerMode");
|
||||||
field.setAccessible(true);
|
field.setAccessible(true);
|
||||||
assertThat((Banner.Mode) field.get(this.application), equalTo(Banner.Mode.OFF));
|
assertThat((Banner.Mode) field.get(this.application), equalTo(Banner.Mode.OFF));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1 +1 @@
|
||||||
spring.main.show_banner=OFF
|
spring.main.banner-mode=off
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue