手把手教你定制Banner

终于我亲手栽下的树引来了乘凉的人,我站在暴雨中、烈日下,无比清醒。

引言

  自2017年注销CSDN博客账户之后,我便很少在公共平台发表技术类的文章,起初发表这类文章的目的很纯粹:分享、记录、成长。后来我渐渐意识到,自己写的文章并没有多大的正面效益。于是后来,我更注重的是个人的沉淀,便注销了该账户,也包括公众号之前发表的一些文章——当然,也存在部分个人原因导致的摆烂心态。

手把手教你定制Banner

但随着新技术的不断推出,以及兼容性问题导致之前总结的文章的准确性和有效性得不到保证,所以后面的博文发表频率或许会得以提升,加油!

  不知道各位在初次见到Vue启动控制台的时候,是否曾与我一样,好奇这种打印ip+port的访问方式感到好奇:

手把手教你定制Banner

然后又想到Spring启动的时候只是打印个版本信息:

手把手教你定制Banner

  其实,或许大家不知道的是Spring也提供了相关的API以供开发人员定制属于自己的启动Banner

正文

  下面给大家献丑介绍几种定制Banner的方法。

1.通过banner.txt文件

推荐一个生成banner.txt文件的地址:https://www.bootschool.net/ascii

只需要将banner.txt文件放在resources目录下即可。

2.通过banner.png文件

推荐一个将图片转成ASII文字的网址:https://www.degraeve.com/img2txt.php

只需要将banner.png文件放在resources目录下即可。

3.以编码方式定制

案例:

手把手教你定制Banner

Spring提供了一个Banner接口用于定制启动Banner,如下代码:

@Builder
public class CustomBanner implements Banner {
    @Override
    public void printBanner(Environment environment, Class<?> sourceClass, PrintStream out) {
        try {
            out.println("33[1;33m ,~~.,''"'`'.~~.    ");
            out.println(": {` .- _ -. '} ;   ");
            out.println(" `:   O(_)O   ;'    ");
            out.println("  ';  ._|_,  ;`     ");
            out.println("   '`-.\_/,.'` cxb  33[0m");
            out.println("33[1;34m======================================启动信息=======================================33[0m");
            // 版本信息
            String jdkVersion = String.valueOf(JavaVersion.getJavaVersion());
            String springVersion = SpringVersion.getVersion();
            String springbootVersion = SpringBootVersion.getVersion();
            // 启动端口、服务路径
            String contextPath = environment.getProperty("server.servlet.context-path");
            String port = environment.getProperty("server.port");
            // 网卡信息
            Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
            out.println("33[1;31mJDK版本号:33[0m" + jdkVersion);
            out.println("33[1;31mSpring版本号:33[0m" + springVersion);
            out.println("33[1;31mSpring Boot版本号:33[0m" + springbootVersion);
            out.println("33[1;31m接口路径:33[0m");
            while (networkInterfaces.hasMoreElements()) {
                NetworkInterface networkInterface = networkInterfaces.nextElement();
                Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
                while (inetAddresses.hasMoreElements()) {
                    InetAddress inetAddress = inetAddresses.nextElement();
                    String ipAddress = inetAddress.getHostAddress();
                    // 排除ipv6协议
                    if (!inetAddress.getHostAddress().contains(":") && !inetAddress.isLoopbackAddress()) {
                        out.println("t Network:http://" + ipAddress + ":" + port + contextPath);
                    } else if (!inetAddress.getHostAddress().contains(":") && inetAddress.isLoopbackAddress()) {
                        out.println("t Localhost:http://" + ipAddress + ":" + port + contextPath);
                    }
                }
            }
            out.println("33[1;31m文档路径:33[0m");
            networkInterfaces = NetworkInterface.getNetworkInterfaces();
            while (networkInterfaces.hasMoreElements()) {
                NetworkInterface networkInterface = networkInterfaces.nextElement();
                Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
                while (inetAddresses.hasMoreElements()) {
                    InetAddress inetAddress = inetAddresses.nextElement();
                    String ipAddress = inetAddress.getHostAddress();
                    if (!inetAddress.getHostAddress().contains(":") && !inetAddress.isLoopbackAddress()) {
                        out.println("t Network:http://" + ipAddress + ":" + port + contextPath + "/doc.html");
                    } else if (!inetAddress.getHostAddress().contains(":") && inetAddress.isLoopbackAddress()) {
                        out.println("t Localhost:http://" + ipAddress + ":" + port + contextPath + "/doc.html");
                    }
                }
            }
            out.println("33[1;34m====================================================================================33[0m");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

然后所需要做的便是修改启动类,覆盖默认的Banner:

SpringApplication app = new SpringApplication(HubBootApplication.class);
app.setBanner(CustomBanner.builder().build());
app.run(args);

拓展

  各位细心的人可能会发现33[1;34m33[0m这几串代码,这其实就是ANSI 转义序列,可以用来定义终端打印字体的属性(颜色、粗体、斜体、背景等)。

ANSI 转义序列的格式如下:

33[<parameter1>;<parameter2>...<parameterN><letter>

  其中,33 表示 ESC,<parameter1><parameter2> 等表示参数,<letter> 表示指令字母。不同的指令字母代表了不同的操作,如下表所示:

指令字母 操作
m 设置文本属性
H 设置光标位置
J 清除屏幕
K 清除行
s 保存光标位置
u 恢复光标位置

在参数中,分号用于分隔不同的参数。常见的参数包括:

参数 意义
0 关闭所有属性
1 设置粗体
2 设置弱化(半亮)
3 设置斜体
4 下划线
5 闪烁(慎用)
7 反显
8 消隐
30-37 设置前景色
40-47 设置背景色

例如,要将文本颜色设置为红色,我们可以使用以下 ANSI 转义序列:

System.out.println("33[31m" + "Hello, world!" + "33[0m")

我也找了一张案例图:

手把手教你定制Banner


原文始发于微信公众号(青衫大叔灬):手把手教你定制Banner

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/179779.html

(0)
小半的头像小半

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!