1、在线生成banner网站

https://www.bootschool.net/ascii
http://www.network-science.de/ascii/
http://patorjk.com/software/taag/
http://www.degraeve.com/img2txt.php

2、两种自定义banner方式

在自定义banner之前,先剖析一下源码,源码跟踪解析如下:

  • springboot启动的main方法
public static void main(string[] args) {
		springapplication springapplication = new springapplication(application.class);
		//开启banner打印方式(off:关闭,console:控制台输出,log:日志输出)
		springapplication.setbannermode(mode.log);
		springapplication.run(args);
	}
  • springapplication.printbanner():
private banner printbanner(configurableenvironment environment) {
       //是否开启banner模式
        if (this.bannermode == mode.off) {
            return null;
        } else {
            resourceloader resourceloader = this.resourceloader != null ? this.resourceloader : new defaultresourceloader((classloader)null);
            springapplicationbannerprinter bannerprinter = new springapplicationbannerprinter((resourceloader)resourceloader, this.banner);
            return this.bannermode == mode.log ? bannerprinter.print(environment, this.mainapplicationclass, logger) : bannerprinter.print(environment, this.mainapplicationclass, system.out);
        }
    }
  • springapplicationbannerprinter.print()
banner print(environment environment, class<?> sourceclass, log logger) {
	   //调用getbanner()方法
        banner banner = this.getbanner(environment);
        try {
            logger.info(this.createstringfrombanner(banner, environment, sourceclass));
        } catch (unsupportedencodingexception var6) {
            logger.warn("failed to create string for banner", var6);
        }
        return new springapplicationbannerprinter.printedbanner(banner, sourceclass);
    }
  • springapplicationbannerprinter.getbanner()
private banner getbanner(environment environment) {
    springapplicationbannerprinter.banners banners = new springapplicationbannerprinter.banners();
    //先获取image类型的banner
    banners.addifnotnull(this.getimagebanner(environment));
    //在获取text类型的banner
    banners.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,如果自定义也没有,则返回默认

那么上述源码中是如何获取image类型和text类型的banner呢?
springapplicationbannerprinter.getimagebanner()
springapplicationbannerprinter.gettextbanner()

//获取text类型的banner
private banner gettextbanner(environment environment) {
    //先从spring.banner.location路径中去取,如果没有,默认banner.txt
    string 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];
            resource resource = this.resourceloader.getresource("banner." + ext);
            if (resource.exists()) {
                return new imagebanner(resource);
            }
        }
        return null;
    }
}

基于图片的 banner

  • 可通过配置参数 spring.banner.image.location 来指定
  • 可将名为 “banner.jpg” (哪几种类型取决于ext常量的定义) 的文件放在classpath 目录(src/main/resources)

基于文件的 banner

  • 可通过配置参数 spring.banner.location 来指定
  • 可将名为 “banner.txt” 的文件放在 classpath 目录(src/main/resources)

源码分析完毕,那么如何去自定义呢?

第一种:配置方式(直接在properties或yml文件中进行配置)
即配置 spring.banner.image.location 和 spring.banner.location路径
或者
将 “图片” 和 “banner.txt” 放在classpath 目录中

spring.banner.location=classpath:banner1.png
spring.banner.image.margin=2
spring.banner.image.height=76
spring.banner.charset=utf-8
spring.banner.image.invert=false
spring.banner.image.location=banner1.png
spring.main.banner-mode=console
spring.main.show-banner=true

第二种:自定义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类
 * @author hb
 * @date 2021-09-09 10:39
 */
public class mybanner implements banner {

    private static final string[] banner = new string[]{"", "  .   ____          _            __ _ _", " /\\\\ / ___'_ __ _ _(_)_ __  __ _ \\ \\ \\ \\", "( ( )\\___ | '_ | '_| | '_ \\/ _` | \\ \\ \\ \\", " \\\\/  ___)| |_)| | | | | || (_| |  ) ) ) )", "  '  |____| .__|_| |_|_| |_\\__, | / / / /", " =========|_|==============|___/=/_/_/_/"};

    public mybanner() {
    }

    @override
    public 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?
springboot启动类中添加自定义banner

@springbootapplication
public class application extends springbootservletinitializer {
   public static void main(string[] args) {
      springapplication springapplication = new springapplication(application.class);
      //添加自定义banner
      springapplication.setbanner(new mybanner());
      springapplication.run(args);
   }
}

如果不想打印banner,可以在启动类的main中,设置 springapplication.setbannermode(banner.mode.off);
或者
通过参数配置spring.main.banner-mode=off来关闭banner的打印

3、控制banner样式

spring提供了三个枚举类来设定字符的颜色,分别是:

ansicolor: 用来设定字符的前景色
ansibackground: 用来设定字符的背景色
ansistyle: 用来控制加粗、斜体、下划线等等。

4、显示应用信息

除了上面的指定样式之外,还可以显示一些与应用相关的版本信息:

${application.version}   与 manifest.mf文件中相同的版本号,比如1.5.4.release
${application.formatted-version}   格式化过的版本号就是加个v然后用括号包起来,比如(v1.5.4.release)
${application.title} 
${spring-boot.version} spring boot的版本
${spring-boot.formatted-version} 格式化过的版本

到此这篇关于springboot之自定义banner详解的文章就介绍到这了,更多相关springboot之自定义banner内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!