SpringBoot之Banner的使用示例
背景
初次运行springboot的小伙伴想必对于springboot打印的banner很感兴趣
spring boot在启动项目时,控制台会打印一个spring的logo。如果不做任何配置
该信息来源于springbootbanner类的静态常量banner,该属性是一个字符串数组,不指定任何banner属性时,控制台默认输出该数组数据。我们可以通过spring boot提供的强大配置功能来改变banner的输出。
通常长成这样
一个spring扑面而来~
那么我们能否定制自己的启动页呢?
源码
springboot在启动时会调用如下一段代码
private banner printbanner(configurableenvironment environment) { if (this.bannermode == banner.mode.off) { return null; } resourceloader resourceloader = this.resourceloader != null ? this.resourceloader : new defaultresourceloader(getclassloader()); springapplicationbannerprinter bannerprinter = new springapplicationbannerprinter( resourceloader, this.banner); if (this.bannermode == mode.log) { return bannerprinter.print(environment, this.mainapplicationclass, logger); } return bannerprinter.print(environment, this.mainapplicationclass, system.out); } public banner print(environment environment, class<?> sourceclass, printstream out) { banner banner = getbanner(environment, this.fallbackbanner); banner.printbanner(environment, sourceclass, out); return new printedbanner(banner, sourceclass); } private banner getbanner(environment environment, banner definedbanner) { banners banners = new banners(); banners.addifnotnull(getimagebanner(environment)); banners.addifnotnull(gettextbanner(environment)); if (banners.hasatleastonebanner()) { return banners; } if (this.fallbackbanner != null) { return this.fallbackbanner; } return default_banner; } private static final banner default_banner = new springbootbanner();
而 springbootbanner长成这样
/** * default banner implementation which writes the 'spring' banner. * * @author phillip webb */ class springbootbanner implements banner { private static final string[] banner = { "", " . ____ _ __ _ _", " /\\\\ / ___'_ __ _ _(_)_ __ __ _ \\ \\ \\ \\", "( ( )\\___ | '_ | '_| | '_ \\/ _` | \\ \\ \\ \\", " \\\\/ ___)| |_)| | | | | || (_| | ) ) ) )", " ' |____| .__|_| |_|_| |_\\__, | / / / /", " =========|_|==============|___/=/_/_/_/" }; private static final string spring_boot = " :: spring boot :: "; private static final int strap_line_size = 42; @override public void printbanner(environment environment, class<?> sourceclass, printstream printstream) { for (string line : banner) { printstream.println(line); } string version = springbootversion.getversion(); version = (version == null ? "" : " (v" + version + ")"); string padding = ""; while (padding.length() < strap_line_size - (version.length() + spring_boot.length())) { padding += " "; } printstream.println(ansioutput.tostring(ansicolor.green, spring_boot, ansicolor.default, padding, ansistyle.faint, version)); printstream.println(); } }
上述代码可以看到banner存在两种 imagebanner和textbanner
imagebanner需要配置属性为banner.image.location
或者支持名为banner格式为 "gif", "jpg", "png的图片
textbanner支持默认情况下在banner.location 或者默认为banner.txt
比如我们可以如下设置
在resource分别放置banner.txt和banner.png
banner.png
banner.txt
/$$$$$$$$ /$$$$$$
| $$_____//$$__ $$
| $$ | $$ \__/
| $$$$$ | $$$$$$$
| $$__/ | $$__ $$
| $$ | $$ \ $$
| $$ | $$$$$$/
|__/ \______/
我们可以看一下结果
哈哈 f6小人变成了这样!!!
关闭banner显示
在程序中可以使用app.setbannermode(mode.off);来关闭banner,利用属性值设置则为:spring.main.banner-mode=off
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: PHP实现的统计数据功能详解
推荐阅读