springboot banner在线生成_SpringBoot教程:SpringBoot配置自定义Banner

一. Spring Boot 常用配置

本章节主要介绍一下 Spring Boot 中的一些常用配置,比如:自定义 Banner、配置日志、关闭特定的自动配置等.

在进行配置之前,我们还是结合之前的文章,先创建一个SpringBoot项目,然后进行本章节的学习。

二. 自定义 Banner

在 Spring Boot 启动的时候会有一个默认的启动图案,被称为Banner。默认的Banner效果如下:

.   ____          _            __ _ _     / / ___'_ __ _ _(_)_ __  __ _        ( ( )___ | '_ | '_| | '_ / _` |         /  ___)| |_)| | | | | || (_| |  ) ) ) )      '  |____| .__|_| |_|_| |___, | / / / /     =========|_|==============|___/=/_/_/_/     :: Spring Boot ::        (v1.5.8.RELEASE)

这个Banner是Spring Boot自带的,如果我们觉得不好看,是可以更改的,作为一个资深的码农,怎么可以不定制一个自己的Banner呢?

1.新建一个banner.txt

我们在 src/main/resources 目录下新建一个 banner.txt

我们可以通过 http://patorjk.com/software/taag 这个网站,来生成自定义的banner字符串,将网站生成的字符复制到 banner.txt 中就可以啦.

2.再次运行这个程序,控制台出现如下界面.

${AnsiColor.BLUE}${spring-boot.version}${spring-boot.formatted-version}//                          _ooOoo_                                                        o8888888o                                                       88" . "88                                                       (| ^_^ |)                                                       O  =  /O                                                    ____/`---'____                                               .'  |     |//  `.                                            /  |||  :  |||//                                            /  _||||| -:- |||||-                                           |   |   -  /// |   |                                         | _|  ''---/''  |   |                                           .-__  `-`  ___/-. /                                       ___`. .'  /--.--  `. . ___                                   ."" '<  `._____/___.'  >'"".                              | | :  `- `.;` _ /`;.`/ - ` : | |                                `-.   _ __ /__ _/   .-` /  /                       ========`-.____`-.________/___.-`____.-'========                                    `=---='                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                    佛祖保佑       永不宕机      永无BUG                  //

3.常用Banner设置属性

  • ${AnsiColor.BRIGHT_RED}: 设置控制台中输出内容的颜色;
  • ${application.version}:用来获取 MANIFEST.MF 文件中的版本号;
  • ${application.formatted-version}: 格式化后的 ${application.version} 版本信息;
  • ${spring-boot.version}: Spring Boot 的版本号;
  • ${spring-boot.formatted-version}: 格式化后的 ${spring-boot.version} 版本信息.
265d452061fe4126b632012b2b5b91a3

4.Banner其他配置

我们可以在application.properties中,进行一些banner的基本属性配置。

我们在src/main/resources目录下,创建一个application.properties配置文件,在该文件中可以添加如下配置信息:

# BANNER#Banner file encoding.spring.banner.charset=UTF-8#Banner text resource location.spring.banner.location=classpath:banner.txt#Banner image file location (jpg or png can also be used).spring.banner.image.location=classpath:banner.gif#Width of the banner image in chars.spring.banner.image.width=76#Height of the banner image in chars (default based on image height).spring.banner.image.height=#Left hand image margin in charsspring.banner.image.margin=2#Whether images should be inverted for dark terminal themes.spring.banner.image.invert=false

这些配置信息,主要是对banner的宽度高度等属性进行设置。

5.关闭Banner

如果我们不想启动项目的时候展示Banner,也可以关闭掉,毕竟这东西没啥作用,典型的属于奇技淫巧。

5.1 代码方式关闭

我们可以在Application入口类中设置Banner的启动模式,默认是开启的,可以关闭掉。

@SpringBootApplicationpublic class BannerApplication {    public static void main(String[] args) {        //SpringApplication.run(BannerApplication.class, args);        SpringApplication application=new SpringApplication(BannerApplication.class);        //设置banner模式,不需要打印banner可以关闭,默认是开启的        application.setBannerMode(Banner.Mode.CONSOLE);        application.run(args);    }}

通过调用setBannerMode()方法,可以设置将banner打印console,log,或者不输出off。

5.2 yml文件配置

我们也可以在yml文件中,设置banner的模式,注意在yml文件中,会将off映射为false,并且需要给off添加括号:

spring:    main:        banner-mode: "off"

5.3 配置方式关闭

这种方式就不用每次都写代码了,可以在每个项目的Edit Configurations中的spring boot选型里,找到Hide Banner,勾选,就可以关闭了!

f37a2512c5ae42a2951cb9253c017537

最终的项目结构,可以参考如下图!

7155547ceb0a4c99a1219ba1d921e93b

私信回复“SpringBoot”获取视频教程大礼包!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值