Disable Spring Boot Banner at Startup – 在启动时禁用Spring Boot Banner

最后修改: 2019年 11月 23日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.绪论

Spring Boot is a great way to create Java web applications, but some of its default behavior may not be ideal for everyone.

Spring Boot是创建Java Web应用程序的绝佳方式,但其某些默认行为可能并不适合所有人。

One particular feature is the Spring Boot banner that gets printed at startup:

一个特别的功能是启动时打印的Spring Boot横幅。

While this banner is typically harmless, in some cases it may be desirable to disable it. For example, to prevent errors with custom logging configurations or save bandwidth with remote log aggregation systems.

虽然这个横幅通常是无害的,但在某些情况下,禁用它可能是可取的。例如,为了防止自定义日志配置的错误,或节省远程日志聚合系统的带宽。

In this tutorial, we will look at some different ways to disable the Spring Boot banner at startup.

在本教程中,我们将研究在启动时禁用Spring Boot横幅的一些不同方法。

2. Using Configuration

2.使用配置

Using configuration is the most flexible way to disable the startup banner. It requires no code changes and can easily be reverted if needed.

使用配置是禁用启动横幅的最灵活方式。它不需要修改代码,而且在需要时可以很容易地恢复

We can disable the startup banner using application.properties:

我们可以使用application.properties禁用启动标语。

spring.main.banner-mode=off

Or if we are using application.yaml:

或者如果我们使用的是application.yaml

spring:
  main:
    banner-mode: "off"

And finally, thanks to Spring Boot’s externalized configuration support, we can also disable it by setting an environment variable:

最后,由于Spring Boot的外部化配置支持,我们还可以通过设置环境变量来禁用它。

SPRING_MAIN_BANNER-MODE=off

3. Using Code

3.使用代码

In addition to configuration, there are also multiple ways to disable the Spring Boot banner using code. The downside to using code is that we need to do this for each application, and it requires a code change to revert.

除了配置之外,还有多种方法可以使用代码禁用Spring Boot横幅。使用代码的缺点是,我们需要为每个应用程序做这件事,而且需要修改代码才能恢复。

When using the SpringApplicationBuilder:

当使用SpringApplicationBuilder

new SpringApplicationBuilder(MyApplication.class)
    .bannerMode(Banner.Mode.OFF)
    .run(args)

And when using SpringApplication:

而当使用SpringApplication时。

SpringApplication app = new SpringApplication(MyApplication.class);
app.setBannerMode(Banner.Mode.OFF);
app.run(args);

4. Using IDE

4.使用IDE

Most modern IDEs include a way to disable the Spring Boot banner without needing configuration or code.

大多数现代IDE包括一种禁用Spring Boot旗帜的方法,不需要配置或代码。

IntelliJ offers a checkbox for Spring Boot run configurations that will disable the banner:

IntelliJ为Spring Boot运行配置提供了一个复选框,将禁用横幅。

5. Change Banner Text

5.改变横幅文字

Another way to disable the Spring Boot startup banner is to change the banner text to an empty file.

禁用Spring Boot启动横幅的另一个方法是将横幅文本改为空文件

We first specify a custom file in application.properties:

我们首先在application.properties中指定一个自定义文件。

spring.banner.location=classpath:/banner.txt

Or, if we’re using YAML:

或者,如果我们使用YAML。

spring:
  banner:
    location: classpath:/banner.txt

Then we create a new empty file in src/main/resources named banner.txt.

然后我们在src/main/resources中创建一个新的空文件,名为banner.txt

6. Conclusion

6.结语

In this tutorial, we’ve seen various ways to disable the Spring Boot banner, using a combination of configuration or code.

在本教程中,我们已经看到了禁用Spring Boot横幅的各种方法,使用了配置或代码的组合。