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} 版本信息.

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 chars
spring.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的启动模式,默认是开启的,可以关闭掉。

@SpringBootApplication
public 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,勾选,就可以关闭了!

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

发布了249 篇原创文章 · 获赞 218 · 访问量 47万+

猜你喜欢

转载自blog.csdn.net/qianfeng_dashuju/article/details/105555306