23.2 Customizing the Banner
通过在classpath下添加一个banner.txt或设置banner.location
来指定相应的文件可以改变启动过程中打印的banner。如果这个文件有特殊的编码,你可以使用banner.encoding
设置它(默认为UTF-8)。除了文本文件,你也可以添加一个banner.gif,banner.jpg或banner.png图片,或设置banner.image.location
属性。图片会转换为字符画(ASCII art)形式,并在所有文本banner上方显示。
在banner.txt中可以使用如下占位符:
Variable | Description |
---|---|
${application.version} | MANIFEST.MF中声明的应用版本号,例如Implementation-Version: 1.0会打印1.0 |
${application.formatted-version} | MANIFEST.MF中声明的被格式化后的应用版本号(被括号包裹且以v作为前缀),用于显示,例如(v1.0) |
${spring-boot.version} | 当前Spring Boot的版本号,例如1.5.7.RELEASE |
${spring-boot.formatted-version} | 当前Spring Boot被格式化后的版本号(被括号包裹且以v作为前缀), 用于显示,例如(v1.5.7.RELEASE) |
${Ansi.NAME}(或\ ${AnsiColor.NAME},\ ${AnsiBackground.NAME}, \ ${AnsiStyle.NAME}) | NAME代表一种ANSI编码,具体详情查看AnsiPropertySource |
${application.title} | MANIFEST.MF中声明的应用title,例如Implementation-Title: MyApp会打印MyApp |
如果想以编程的方式产生一个banner,可以使用
SpringBootApplication.setBanner(…)
方法,并实现org.springframework.boot.Banner
接口的printBanner()
方法。你也可以使用
spring.main.banner-mode
属性决定将banner打印到何处:
- "console"(System.out)
- "log"(配置的logger)
- "off"(都不输出)
打印的
banner
将注册成一个名为springBootBanner
的单例bean。如果采用YAML作为配置文件,需要注意YAML会将off映射为false,如果想在应用中禁用banner,你需要确保off添加了括号:
spring: main: banner-mode: "off"