Spring Boot程序启动的时候,会输出由字符组成的Spring符号,这个就是Spring Boot为自己设计的Banner。
其实我们可以自定义banner:
#1.开启/关闭Banner
## Spring Boot 1.x
* 方法1.在Application的main方法中设置:
```java
SpringApplication application = new SpringApplication(App.class);
/*
* Banner.Mode.OFF:关闭;
* Banner.Mode.CONSOLE:控制台输出,默认方式;
* Banner.Mode.LOG:日志输出方式;
*/
application.setBannerMode(Banner.Mode.OFF);
application.run(args);
```
* 方法2.在 `application.properties` 进行配置 banner 的显示和关闭:
```properties
### 是否显示banner,可选值[console|off|log]
spring.main.banner-mode=console
```
商标 ( banner ) 属性
```properties
# Spring 主函数商标选项.
# console: System.out 输出,默认值
# log: 将商标打印到日志中
# off: 关闭
spring.main.banner-mode=console
#### BANNER 属性配置 ####
# 商标文件编码
banner.charset=UTF-8
# 商标文件路径
banner.locattion=classpath:banner.txt
# 商标文件路径,文件名也可以是 banner.jpg, banner. png
banner.image.location=classpath:banner.gif
# 商标文字宽度(默认是 76 个字符 〕
banner.image.width=
# 商标高度(默认是商标图片默认的高度)
banner.image.height=
# 左手边图片商标的页边距(默认为 2)
banner.image.margin=
# 布尔值,是否将图像反转为黑色主题,即白色转换为黑色, 黑色转换为白色(默认值 false)
banner.image.invert=false
```
## Spring Boot 2.x
商标 ( banner ) 属性
```properties
# Spring 主函数商标选项.
# console: System.out 输出,默认值
# log: 将商标打印到日志中
# off: 关闭
spring.main.banner-mode=console
#### BANNER 属性配置 ####
# 商标文件编码
spring.banner.charset=UTF-8
# 商标文件路径
spring.banner.locattion=classpath:banner.txt
# 商标文件路径,文件名也可以是 banner.jpg, banner. png
spring.banner.image.location=classpath:banner.gif
# 商标文字宽度(默认是 76 个字符 〕
spring.banner.image.width=
# 商标高度(默认是商标图片默认的高度)
spring.banner.image.height=
# 左手边图片商标的页边距(默认为 2)
spring.banner.image.margin=
# 布尔值,是否将图像反转为黑色主题,即白色转换为黑色, 黑色转换为白色(默认值 false)
spring.banner.image.invert=false
```
所以关闭商标的功能只需要设置``spring.main.banner-mode=off``即可。
#2.自定义anner内容
* 方法1.修改banner.txt配置文件
在 `src/main/resouces` 下新建 `banner.txt`,在文件中加入自定义内容即可
其实 Banner 启动界面一个很重要的功能就是要告诉我们这个项目的一些重要信息,下面是一些可通过 `${}` 放入 `banner.txt` 中的信息。这样可以就打印出对应的信息。
```
#这个是MANIFEST.MF文件中的版本号
${application.version}
#这个是上面的的版本号前面加v后上括号
${application.formatted-version}
#这个是springboot的版本号
${spring-boot.version}
#这个是springboot的版本号
${spring-boot.formatted-version}
#设置控制台中输出内容的颜色
${AnsiColor.BRIGHT_RED}
```
* 方法2.重写接口Banner实现
SpringBoot 提供了一个接口 `org.springframework.boot.Banner`,他的实例可以被传给`SpringApplication` 的 `setBanner(banner)` 方法。如果你闲得不行非要着重美化这个命令行输出的话,可以重写 `Banner` 接口的 `printBanner` 方法。
推荐第1种修改方法。
# 3.生成自定义ANSIC字符画
推荐几个字符画生成的网站,我们可以利用生成的字符串放入这个 `banner.txt` 文件:
http://www.network-science.de/ascii/
http://patorjk.com/software/taag/
http://www.degraeve.com/img2txt.php
# 4.Banner美化
其实Spring Boot还为这个彩蛋提供了不少美化功能: 提供了一个枚举类 `AnsiColor`,这个类可以控制`banner.txt` 中的字符颜色,而且非常容易使用。
如果颜色没有变,那么还需要设置:`spring.output.ansi.enabled=ALWAYS`
提供一个完整的demo~
```bash
${AnsiColor.BRIGHT_GREEN}
████████╗███████╗███████╗████████╗
╚══██╔══╝██╔════╝██╔════╝╚══██╔══╝
██║ █████╗ ███████╗ ██║
██║ ██╔══╝ ╚════██║ ██║
██║ ███████╗███████║ ██║
╚═╝ ╚══════╝╚══════╝ ╚═╝
${AnsiColor.BRIGHT_RED}
Application Version: ${application.version}${application.formatted-version}
Spring Boot Version: ${spring-boot.version}${spring-boot.formatted-version}
```
Spring Boot学习2——自定义banner