javascript
SpringBoot2.x系列教程10--小花样之SpringBoot配置自定义Banner
SpringBoot系列教程10--小花樣之SpringBoot配置自定義Banner
作者:一一哥
一. Spring Boot 常用配置
本章節(jié)主要介紹一下 Spring Boot 中的一些常用配置,比如:自定義 Banner、配置日志、關(guān)閉特定的自動(dòng)配置等.
在進(jìn)行配置之前,我們還是結(jié)合之前的文章,先創(chuàng)建一個(gè)SpringBoot項(xiàng)目,然后進(jìn)行本章節(jié)的學(xué)習(xí)。
二. 自定義 Banner
在 Spring Boot 啟動(dòng)的時(shí)候會(huì)有一個(gè)默認(rèn)的啟動(dòng)圖案,被稱為Banner。
默認(rèn)的Banner效果如下:
這個(gè)Banner是Spring Boot自帶的,如果我們覺得不好看,是可以更改的,作為一個(gè)資深的碼農(nóng),怎么可以不定制一個(gè)自己的Banner呢?
1.新建一個(gè)banner.txt
我們?cè)?src/main/resources?目錄下新建一個(gè) banner.txt
我們可以通過?http://patorjk.com/software/taag?這個(gè)網(wǎng)站,來生成自定義的banner字符串,將網(wǎng)站生成的字符復(fù)制到 banner.txt 中就可以啦.
2.再次運(yùn)行這個(gè)程序,控制臺(tái)出現(xiàn)如下界面.
${AnsiColor.BLUE} ${spring-boot.version} ${spring-boot.formatted-version}// _ooOoo_ // // o8888888o // // 88" . "88 // // (| ^_^ |) // // O\ = /O // // ____/`---'\____ // // .' \\| |// `. // // / \\||| : |||// \ // // / _||||| -:- |||||- \ // // | | \\\ - /// | | // // | \_| ''\---/'' | | // // \ .-\__ `-` ___/-. / // // ___`. .' /--.--\ `. . ___ // // ."" '< `.___\_<|>_/___.' >'"". // // | | : `- \`.;`\ _ /`;.`/ - ` : | | // // \ \ `-. \_ __\ /__ _/ .-` / / // // ========`-.____`-.___\_____/___.-`____.-'======== // // `=---=' // // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ // // 佛祖保佑 永不宕機(jī) 永無BUG //3.常用Banner設(shè)置屬性
- ${AnsiColor.BRIGHT_RED}: 設(shè)置控制臺(tái)中輸出內(nèi)容的顏色;
- ${application.version}:用來獲取?MANIFEST.MF?文件中的版本號(hào);
- ${application.formatted-version}: 格式化后的?${application.version}?版本信息;
- ${spring-boot.version}: Spring Boot 的版本號(hào);
- ${spring-boot.formatted-version}: 格式化后的?${spring-boot.version}?版本信息.
4.Banner其他配置
我們可以在application.properties中,進(jìn)行一些banner的基本屬性配置。
我們?cè)趕rc/main/resources目錄下,創(chuàng)建一個(gè)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這些配置信息,主要是對(duì)banner的寬度高度等屬性進(jìn)行設(shè)置。
5.關(guān)閉Banner
如果我們不想啟動(dòng)項(xiàng)目的時(shí)候展示Banner,也可以關(guān)閉掉,畢竟這東西沒啥作用,典型的屬于奇技淫巧。
5.1 代碼方式關(guān)閉
我們可以在Application入口類中設(shè)置Banner的啟動(dòng)模式,默認(rèn)是開啟的,可以關(guān)閉掉。
@SpringBootApplication public class BannerApplication {public static void main(String[] args) {//SpringApplication.run(BannerApplication.class, args);SpringApplication application=new SpringApplication(BannerApplication.class);//設(shè)置banner模式,不需要打印banner可以關(guān)閉,默認(rèn)是開啟的application.setBannerMode(Banner.Mode.CONSOLE);application.run(args);} }通過調(diào)用setBannerMode()方法,可以設(shè)置將banner打印console,log,或者不輸出off。
5.2 yml文件配置
我們也可以在yml文件中,設(shè)置banner的模式,注意在yml文件中,會(huì)將off映射為false,并且需要給off添加括號(hào):
spring:main:banner-mode: "off"5.3 配置方式關(guān)閉
這種方式就不用每次都寫代碼了,可以在每個(gè)項(xiàng)目的Edit Configurations中的spring boot選型里,找到Hide Banner,勾選,就可以關(guān)閉了!
最終的項(xiàng)目結(jié)構(gòu),可以參考如下圖!
總結(jié)
以上是生活随笔為你收集整理的SpringBoot2.x系列教程10--小花样之SpringBoot配置自定义Banner的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 基于DirectShow的简单播放器
- 下一篇: 万字总结 JS 数据结构与常用的算法