# 23.2. 自定义Banner

通过在classpath下添加一个`banner.txt`或设置`spring.banner.location`属性来指定相应的文件，可以改变启动过程中打印的banner。如果这个文件不是以UTF-8编码，你可以设置`spring.banner.charset`。除了文本文件，你也可以添加一个`banner.gif`，`banner.jpg`或`banner.png`图片，或设置`spring.banner.image.location`属性。图片会转换为字符画（ASCII art）形式，并在所有文本banner上方显示。

在banner.txt中可以使用如下占位符：

| 变量                                                                         | 描述                                                                                                                                                                                            |
| -------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| ${application.version}                                                     | MANIFEST.MF中声明的应用版本号，例如`Implementation-Version: 1.0`会打印`1.0`                                                                                                                                  |
| ${application.formatted-version}                                           | MANIFEST.MF中声明的被格式化后的应用版本号（被括号包裹且以v作为前缀），用于显示，例如(`v1.0`)                                                                                                                                      |
| ${spring-boot.version}                                                     | 当前Spring Boot的版本号，例如`2.0.0.RELEASE`                                                                                                                                                           |
| ${spring-boot.formatted-version}                                           | 当前Spring Boot被格式化后的版本号（被括号包裹且以v作为前缀）,  用于显示，例如(`v2.0.0.RELEASE`)                                                                                                                              |
| ${Ansi.NAME}（或${AnsiColor.NAME}，${AnsiBackground.NAME}, ${AnsiStyle.NAME}） | NAME代表一种ANSI编码，具体详情查看[AnsiPropertySource](https://github.com/spring-projects/spring-boot/tree/v2.0.0.RELEASE/spring-boot/src/main/java/org/springframework/boot/ansi/AnsiPropertySource.java) |
| ${application.title}                                                       | `MANIFEST.MF`中声明的应用title，例如`Implementation-Title: MyApp`会打印`MyApp`                                                                                                                            |

**注** 如果想以编程的方式产生一个banner，可以使用`SpringBootApplication.setBanner(…)`方法，并实现`org.springframework.boot.Banner`接口的`printBanner()`方法。

你也可以使用`spring.main.banner-mode`属性决定将banner打印到何处，`System.out`（`console`），配置的logger（`log`）或都不输出（`off`)。

打印的banner将注册成一个名为`springBootBanner`的单例bean。

**注** YAML会将`off`映射为`false`，如果想在应用中禁用banner，你需要确保`off`添加了引号：

```javascript
spring:
    main:
        banner-mode: "off"
```
