SpringBoot自定义banner,如何定制炫酷的banner提升项目B格?
文章目录
- 写在前面
- 自定义banner
- 使用banner.txt文件
- 使用图片
- 手写一个banner
- banner参数
- 在 application.properties 文件中可以配置banner其他属性
- banner自身参数
- 源码分析
- 在线生成banner
写在前面
Springboot启动的时候默认是有一套自己的banner的:
我们如何自定义这个banner呢?
自定义banner
使用banner.txt文件
在Spring Boot工程的/src/main/resources目录下创建一个banner.txt文件,然后将ASCII字符画复制进去,就能替换默认的banner了。
banner.txt:
_ _ _ _
(_)(_)(_)(_)(_) (_)_ _ _ _ _ _ _ _ _ _ _ _(_) (_) (_)(_)(_)(_)_ (_)(_)_(_)(_) _ (_)(_)(_) _(_) (_)(_) _ _ _ (_)(_) (_) (_)(_) (_)(_) _(_)(_)(_)(_)(_)(_)(_) (_) (_)(_) (_)(_)_ _ (_) (_)_ _ _ _ (_) (_) (_)(_) _ _ _ (_)
(_)(_)(_)(_) (_)(_)(_)(_) (_) (_) (_) (_)(_)(_)
启动后的打印效果:
使用图片
在Spring Boot工程的/src/main/resources目录下,放置一张图片,起名为banner.xxx(其中xxx为gif、jpg、png格式),在项目启动时会自动解析该图片。
banner.jpg:
启动后的打印效果:
注意:图片如果太花了,效果可能并不会很好。
手写一个banner
import org.springframework.boot.Banner;
import org.springframework.boot.ansi.AnsiColor;
import org.springframework.boot.ansi.AnsiOutput;
import org.springframework.boot.ansi.AnsiStyle;
import org.springframework.core.env.Environment;
import java.io.PrintStream;
/** 自定义banner类*/
public class MyBanner implements Banner {private static final String[] BANNER = new String[]{"", " . ____ _ __ _ _", " /\\\\ / ___'_ __ _ _(_)_ __ __ _ \\ \\ \\ \\", "( ( )\\___ | '_ | '_| | '_ \\/ _` | \\ \\ \\ \\", " \\\\/ ___)| |_)| | | | | || (_| | ) ) ) )", " ' |____| .__|_| |_|_| |_\\__, | / / / /", " =========|_|==============|___/=/_/_/_/"};public MyBanner() {}@Overridepublic void printBanner(Environment environment, Class<?> sourceClass, PrintStream out) {String[] bannerArray = BANNER;int bannerLength = bannerArray.length;for(int i = 0; i < bannerLength; ++i) {String line = bannerArray[i];out.println(line);}out.println(AnsiOutput.toString(new Object[]{AnsiColor.GREEN, " :: Spring Boot :: ", AnsiColor.DEFAULT, AnsiStyle.FAINT}));out.println();}
}// 启动类中加入banner即可
@SpringBootApplication
public class DemoApplication {public static void main(String[] args) {SpringApplication springApplication = new SpringApplication(DemoApplication.class);//添加自定义bannerspringApplication.setBanner(new MyBanner());springApplication.run(args);}}
banner参数
在 application.properties 文件中可以配置banner其他属性
# 来确定横幅是必须在控制台(console)上打印、发送到配置的记录器(log)还是根本不生成(off)。
spring.main.banner-mode=off
# 在 application.properties 文件中可以配置banner属性
spring.banner.charset=UTF-8
spring.banner.location=classpath:banner.txt
#在 application.properties 文件中可以配置图片的高度、宽度、颜色深度
spring.banner.image.width=100
spring.banner.image.height=20
spring.banner.image.bitdepth=4
# 是否应该为黑暗终端主题反转图像
spring.banner.image.invert=true
# banner图片的位置
spring.banner.image.location=classpath:banner.jpg
# banner右移字符数
spring.banner.image.margin=2
banner自身参数
参数 | 描述 |
---|---|
${application.version} | MANIFEST.MF中声明的应用程序的版本号 |
${application.formatted-version} | MANIFEST.MF声明的应用程序的版本号并格式化以供显示(用括号括起来并以v为前缀)。比如(v1.0)。 |
${spring-boot.version} | 你正在使用的Spring Boot版本 |
${Ansi.NAME} (or ${AnsiColor.NAME}, ${AnsiBackground.NAME}, ${AnsiStyle.NAME}) | 其中NAME是ANSI转义码的名称 |
${application.title} | 在MANIFEST.MF中声明的应用程序的标题。 |
${AnsiColor.BRIGHT_RED} | 设置控制台中输出内容的颜色 |
${…} | 其他任意配置信息,比如说${server.port}获取端口 |
源码分析
1、在SpringApplication的run方法中,一直点进去,可以看到这样一段逻辑:
// org.springframework.boot.SpringApplication#run(java.lang.String...)
public ConfigurableApplicationContext run(String... args) {long startTime = System.nanoTime();DefaultBootstrapContext bootstrapContext = createBootstrapContext();ConfigurableApplicationContext context = null;configureHeadlessProperty();SpringApplicationRunListeners listeners = getRunListeners(args);listeners.starting(bootstrapContext, this.mainApplicationClass);try {ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);ConfigurableEnvironment environment = prepareEnvironment(listeners, bootstrapContext, applicationArguments);configureIgnoreBeanInfo(environment);// 打印bannerBanner printedBanner = printBanner(environment);context = createApplicationContext();context.setApplicationStartup(this.applicationStartup);prepareContext(bootstrapContext, context, environment, listeners, applicationArguments, printedBanner);refreshContext(context);afterRefresh(context, applicationArguments);Duration timeTakenToStartup = Duration.ofNanos(System.nanoTime() - startTime);if (this.logStartupInfo) {new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), timeTakenToStartup);}listeners.started(context, timeTakenToStartup);callRunners(context, applicationArguments);}catch (Throwable ex) {handleRunFailure(context, ex, listeners);throw new IllegalStateException(ex);}try {Duration timeTakenToReady = Duration.ofNanos(System.nanoTime() - startTime);listeners.ready(context, timeTakenToReady);}catch (Throwable ex) {handleRunFailure(context, ex, null);throw new IllegalStateException(ex);}return context;
}
其中printBanner就是打印banner的代码。
2、printBanner方法
// org.springframework.boot.SpringApplication#printBanner
private Banner printBanner(ConfigurableEnvironment environment) {// 判断banner的模式if (this.bannerMode == Banner.Mode.OFF) {return null;}ResourceLoader resourceLoader = (this.resourceLoader != null) ? this.resourceLoader: new DefaultResourceLoader(null);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);
}
3、控制台print方法
// org.springframework.boot.SpringApplicationBannerPrinter#print(org.springframework.core.env.Environment, java.lang.Class<?>, java.io.PrintStream)Banner print(Environment environment, Class<?> sourceClass, PrintStream out) {// 获取BannerBanner banner = getBanner(environment);// 打印Bannerbanner.printBanner(environment, sourceClass, out);return new PrintedBanner(banner, sourceClass);}
4、getBanner
// org.springframework.boot.SpringApplicationBannerPrinter#getBanner
private Banner getBanner(Environment environment) {SpringApplicationBannerPrinter.Banners banners = new SpringApplicationBannerPrinter.Banners();//先获取image类型的bannerbanners.addIfNotNull(this.getImageBanner(environment));//在获取text类型的bannerbanners.addIfNotNull(this.getTextBanner(environment));if (banners.hasAtLeastOneBanner()) {// 如果至少有一个,则返回// Banners 也实现了 Banner 接口,运用了组合模式,实际上可同时打印图片和文本 banner。return banners;} else {// 返回自定义的banner(this.fallbackBanner) 或者 springboot默认的banner(DEFAULT_BANNER)// 默认的banner类:SpringBootBanner。// 自定义的banner:需要我们仿照SpringBootBanner去自定义一个类//this.fallbackBanner: 表示自定义的banner,此参数可在springboot启动类的main方法中设置,后续会介绍// public static void main(String[] args) {// SpringApplication springApplication = new SpringApplication(Application.class);// springApplication.setBanner(new MyBanner());//自定义的banner// springApplication.run(args);// }return this.fallbackBanner != null ? this.fallbackBanner : DEFAULT_BANNER;}
}
banner的获取方式有两种,先获取image类型的banner,然后获取text类型的banner,如果至少有一个,则执行该banner,如果没有,返回自定义的banner,如果自定义也没有,则返回默认
5、获取banner
//获取Text类型的banner
private Banner getTextBanner(Environment environment) {//先从spring.banner.location路径中去取,如果没有,默认banner.txtString location = environment.getProperty("spring.banner.location", "banner.txt");Resource resource = this.resourceLoader.getResource(location);try {if (resource.exists() && !resource.getURL().toExternalForm().contains("liquibase-core")) {return new ResourceBanner(resource);}} catch (IOException var5) {}return null;
}//获取image类型的banner
private Banner getImageBanner(Environment environment) {String location = environment.getProperty("spring.banner.image.location");if (StringUtils.hasLength(location)) {Resource resource = this.resourceLoader.getResource(location);return resource.exists() ? new ImageBanner(resource) : null;} else {String[] var3 = IMAGE_EXTENSION;int var4 = var3.length;for(int var5 = 0; var5 < var4; ++var5) {String ext = var3[var5];// static final String[] IMAGE_EXTENSION = { "gif", "jpg", "png" };Resource resource = this.resourceLoader.getResource("banner." + ext);if (resource.exists()) {return new ImageBanner(resource);}}return null;}
}
在线生成banner
这里提供了几个在线生成banner的网址:
https://www.bootschool.net/ascii
http://www.network-science.de/ascii/
http://patorjk.com/software/taag/
http://www.degraeve.com/img2txt.php
SpringBoot自定义banner,如何定制炫酷的banner提升项目B格?相关推荐
- swiper炫酷_swiper3d横向滚动多张炫酷切换banner
最近有了个新需求,swiper3d横向滚动多张炫酷切换banner要和elementUI里边走马灯的卡片化card 类似,但是还需要h5手机触摸滚动啊啊啊啊,昨天折腾了半个早上总算完成,今天乖乖跑来m ...
- python炫酷特效代码_推荐几个炫酷的 Python 开源项目
推荐几个炫酷的 Python 开源项目 项目一: Supervisor 简介: Supervisor 是实际企 业常用的一款 Linux/Unix 系统下的一个进程管理工具, 基于 Python 开发 ...
- 推荐几个炫酷的Python开源项目
项目一: Supervisor 简介:Supervisor是实际企业常用的一款 Linux/Unix 系统下的一个进程管理工具,基于Python开发,可以很方便的监听.启动.停止.重启一个或多个进程, ...
- jupyter代码字体大小_你可能并不知道这样定制炫酷的jupyter主题
之前用多了mac,在windows上使用jupyter notebook的时候,总觉得界面不是很舒服,见下图,尤其是字体,看着挺难受的,严重影响了使用的心情哈哈哈.那这样"丑"的界 ...
- jupyter notebook 快捷键设置字体大小_你可能并不知道这样定制炫酷的jupyter主题
之前用多了mac,在windows上使用jupyter notebook的时候,总觉得界面不是很舒服,见下图,尤其是字体,看着挺难受的,严重影响了使用的心情哈哈哈.那这样"丑"的界 ...
- html广告条效果,css3炫酷网站banner广告动画特效
这是一款可以用来遮罩网站banner或广告的动画特效插件.该特效使用的是 CSS3 animations.注意不是所有的浏览器都支持 CSS3 animations.如果你对 CSS3 animati ...
- 球体动画Android,Android自定义View实现简单炫酷的球体进度球实例代码
前言 最近一直在研究自定义view,正好项目中有一个根据下载进度来实现球体进度的需求,所以自己写了个进度球,代码非常简单.先看下效果: 效果还是非常不错的. 准备知识 要实现上面的效果我们只要掌握两个 ...
- 自定义View实现Canvas炫酷效果
效果: 整个效果分为旋转.扩散聚合.水波纹效果,首先在定义好一些变量后,要先定义一个抽象类SplashState,提供抽象方法drawState供子类实现. /*** 这个抽象类,对外提供drawSt ...
- android 自定义特效,Android自定义View:实现炫酷的点赞特效
闲暇时间,看到直播软件都有点赞的爆炸效果,所以也就试着写了一个点赞效果,写的不好亲们勿怪! 这里只是简单说明,具体可查看源码:可查看源码 演示如下: 分析: 1.开始加载一个心形View 2.点击心形 ...
最新文章
- 【OpenGL】二十四、OpenGL 纹理贴图 ( 读取文件内容 | 桌面程序添加控制台窗口 | ‘fopen‘: This function may be unsafe 错误处理 )
- linux Centos7下安装python3及pip3
- oracle表查询不动怎么转储,Oracle常用的转储方法总结
- 【NLP】图解 BERT 预训练模型!
- 十大WordPress安全设置技巧
- C#中几种代码复用的方式
- php fpm 日志记录,使用Nginx在PHP-FPM 7上启用错误日志记录?
- shell批量文件编码转换
- Windows环境下使用CMake编译OpenCV3.0和OpenCV_contrib
- at24c08 E2PROM的I2C设备驱动实例——基于mini2440
- python词云制作(最全最详细的教程)
- HarmonyOS APP开发入门3——组件(二 Text组件)
- 74LVC2G14GW 封装 SOT363 栅极/逆变器芯片
- CMDN Club每周精选(第4期)
- 计算机网络基础之计算机网络
- CSS3——2D变形(CSS3) transform
- 28款静态网站快速搭建生成器
- 我的世界java活板门会被烧没_《我的世界》新版1.14的活板门特性改变了?玩家开发出新的玩法!...
- 对话知道创宇丨如何守住内容安全生命线?
- Lesson14 Redis集群的搭建
热门文章
- 红楼梦人物出场统计python_红楼梦有多少人物统计(一)
- webdriver options常用参数
- [附源码]java毕业设计医疗预约系统
- 芯片在计算机中作用是什么,芯片的主要作用
- word单元格自动换行,适应文字
- pythongui库推荐_八款常用的 Python GUI 开发框架推荐
- android 点击退出账号,安卓退出登录功能
- 二次元究竟招惹了谁?谣言煽动背后的文化迷思,警惕有可能发生的思想劫持【文明启示录#01】【补档】
- 数据分析——1.环境搭建(Jupyter Lab安装教程)
- 雍正王朝里康熙临终予四爷言