Spring Boot 通过代码动态生成 Banner

本文将介绍怎样实现 Banner 接口,通过代码动态生成自己的 Banner

Spring Boot 中横幅(Banner)是通过实现 Banner 接口来实现的,它默认提供了 SpringBootBanner 类,该类输出一个“Spring Boot”字符图和 Spring Boot 版本信息。如下:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.4.RELEASE)

我们可以通过实现 Banner 接口,输出我们自己想要的 Banner。假如我们自定义 Banner 为 MyBanner,代码如下:

package com.huangx.springboot.springboot_banner;

import org.springframework.boot.Banner;
import org.springframework.core.env.Environment;
import java.io.PrintStream;

/**
 * 自定义自己的 Banner
 */
public class MyBanner implements Banner {

    private static final String[] BANNER = {
            "  _    ___   __ _____ _______ _____  _______      ________",
            " | |  | \\ \\ / // ____|__   __|  __ \\|_   _\\ \\    / /  ____|",
            " | |__| |\\ V /| (___    | |  | |__) | | |  \\ \\  / /| |__",
            " |  __  | > <  \\___ \\   | |  |  _  /  | |   \\ \\/ / |  __|",
            " | |  | |/ . \\ ____) |  | |  | | \\ \\ _| |_   \\  /  | |____",
            " |_|  |_/_/ \\_\\_____/   |_|  |_|  \\_\\_____|   \\/   |______|"
    };

    @Override
    public void printBanner(Environment environment, Class<?> sourceClass, PrintStream out) {
        for(String line : BANNER) {
            out.println(line);
        }
        out.flush();
    }

}

上面自定义的 Banner 仅仅输出“hxstrive”字符图。

有了我们自定义的 Banner,此时需要利用 SpringApplicationBuilder 类将我们自定义的 Banner 注入到 Spring Boot 中,替换默认的 SpringBootBanner。代码如下:

package com.huangx.springboot.springboot_banner;

import org.springframework.boot.Banner;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;

@SpringBootApplication
public class SpringbootBannerApplication {

    public static void main(String[] args) {
        new SpringApplicationBuilder(SpringbootBannerApplication.class)
                .banner(new MyBanner())
                .bannerMode(Banner.Mode.CONSOLE).run(args);
    }

}

运行结果:

  _    ___   __ _____ _______ _____  _______      ________
 | |  | \ \ / // ____|__   __|  __ \|_   _\ \    / /  ____|
 | |__| |\ V /| (___    | |  | |__) | | |  \ \  / /| |__
 |  __  | > <  \___ \   | |  |  _  /  | |   \ \/ / |  __|
 | |  | |/ . \ ____) |  | |  | | \ \ _| |_   \  /  | |____
 |_|  |_/_/ \_\_____/   |_|  |_|  \_\_____|   \/   |______|
2020-10-17 20:47:09.868  INFO 492 --- [  restartedMain] c.h.s.s.SpringbootBannerApplication      : Starting SpringbootBannerApplication on MS-OYCYMLXUSLLD with PID 492 (D:\learn\Spring\springboot\springboot_workspaces\springboot2\springboot_banner\target\classes started by Administrator in D:\learn\Spring\springboot\springboot_workspaces\springboot2\springboot_banner)
2020-10-17 20:47:09.874  INFO 492 --- [  restartedMain] c.h.s.s.SpringbootBannerApplication      : No active profile set, falling back to default profiles: default
2020-10-17 20:47:10.061  INFO 492 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2020-10-17 20:47:10.061  INFO 492 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2020-10-17 20:47:12.475  INFO 492 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-10-17 20:47:12.519  INFO 492 --- [  restartedMain] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-10-17 20:47:12.520  INFO 492 --- [  restartedMain] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.17]
2020-10-17 20:47:12.699  INFO 492 --- [  restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-10-17 20:47:12.699  INFO 492 --- [  restartedMain] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2637 ms
2020-10-17 20:47:12.976  INFO 492 --- [  restartedMain] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-10-17 20:47:13.192  INFO 492 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
2020-10-17 20:47:13.276  INFO 492 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-10-17 20:47:13.281  INFO 492 --- [  restartedMain] c.h.s.s.SpringbootBannerApplication      : Started SpringbootBannerApplication in 4.157 seconds (JVM running for 6.918)

你还可以在代码做一些动态显示,例如:显示当前时间、软件版本、JDK版本、系统环境等信息。

所谓天才,只不过是把别人喝咖啡的功夫都用在工作上了。——鲁迅
0 不喜欢
说说我的看法 -
全部评论(
没有评论
关于
本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,请来信告知:hxstrive@outlook.com
公众号