是时候让你的程序有点春节的味道了

2022!虎年来了~ 我们来看看作为一个后端开发如何研究下怎么给我们的IDEA 添加点过年滴气氛,像这样:2022 happy

image.png

✏️springboot自定义banner方式

方法1:根路径banner.txt

在resources下新建banner.txt文件,并复制banner内容进去,springboot启动启动就会读取该文件进行打印。

Spring Boot banner在线生成工具

image.png

banner.txt内容如下:

  ████   ████   ████   ████    ██                                        
 █░░░ █ █░░░██ █░░░ █ █░░░ █  ░██                ██████  ██████   ██   ██
░    ░█░█  █░█░    ░█░    ░█  ░██       ██████  ░██░░░██░██░░░██ ░░██ ██ 
   ███ ░█ █ ░█   ███    ███   ░██████  ░░░░░░██ ░██  ░██░██  ░██  ░░███  
  █░░  ░██  ░█  █░░    █░░    ░██░░░██  ███████ ░██████ ░██████    ░██   
 █     ░█   ░█ █      █       ░██  ░██ ██░░░░██ ░██░░░  ░██░░░     ██    
░██████░ ████ ░██████░██████  ░██  ░██░░████████░██     ░██       ██     
░░░░░░  ░░░░  ░░░░░░ ░░░░░░   ░░   ░░  ░░░░░░░░ ░░      ░░       ░░      
复制代码

启动springboot项目打印:

image.png

方法2:自定义banner图片

我们可以使用gif类型的图片文件,让springboot启动时安帧多打印几次banner,准备gif文件

image.png

  • banner1.gif:

banner1.gif

  • fireworks.gif:

fireworks.gif

配置gif路径

spring:
  banner:
    image:
      location: classpath:myBanner/banner1.gif
#      location: classpath:myBanner/fireworks.gif
复制代码

fireworks.gif启动效果:

  省略。。。
   
  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@8@@@@&@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@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@@@@@@@@@@@@@@@@@@@@@@@@
  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@.@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

  ████   ████   ████   ████    ██
 █░░░ █ █░░░██ █░░░ █ █░░░ █  ░██                ██████  ██████   ██   ██
░    ░█░█  █░█░    ░█░    ░█  ░██       ██████  ░██░░░██░██░░░██ ░░██ ██
   ███ ░█ █ ░█   ███    ███   ░██████  ░░░░░░██ ░██  ░██░██  ░██  ░░███
  █░░  ░██  ░█  █░░    █░░    ░██░░░██  ███████ ░██████ ░██████    ░██
 █     ░█   ░█ █      █       ░██  ░██ ██░░░░██ ░██░░░  ░██░░░     ██
░██████░ ████ ░██████░██████  ░██  ░██░░████████░██     ░██       ██
░░░░░░  ░░░░  ░░░░░░ ░░░░░░   ░░   ░░  ░░░░░░░░ ░░      ░░       ░░

2022-01-10 11:23:15.146  INFO 109632 --- [           main] com.ljw.Application                      : Starting Application using Java 1.8.0_202 on LAPTOP-HTA2F6RV with PID 109632 (E:\project\private\github\java-guide\banner-spring-boot\target\classes started by ljw in E:\project\private\github\java-guide)
2022-01-10 11:23:15.148  INFO 109632 --- [           main] com.ljw.Application                      : No active profile set, falling back to default profiles: default
2022-01-10 11:23:15.436  INFO 109632 --- [           main] com.ljw.Application                      : Started Application in 3.027 seconds (JVM running for 3.643)

Process finished with exit code 0

复制代码

banner1.gif启动效果:

  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  @@@@@@@:                                                              @@@@@@
  @@@@@@@*                                                              @@@@@@
  @@@@@@@*                                                              @@@@@@
  @@@@@@@*                                                              @@@@@@
  @@@@@@@*                                                              @@@@@@
  @@@@@@@*              #@@@@@   @@@@@.   @@@@@    @@@@@                @@@@@@
  @@@@@@@*              @   #@  @@   o@& #    @@  @    @@               @@@@@@
  @@@@@@@*                 @@8 @@     @@    @@@      @@@                @@@@@@
  @@@@@@@*              @@@    &@     @@  @@&     &@@                   @@@@@@
  @@@@@@@*             #@       @@   @@  @@       @#                    @@@@@@
  @@@@@@@*              @@@@@@   o@@@#   @@@@@@@  @@@@@@:               @@@@@@
  @@@@@@@*                                                              @@@@@@
  @@@@@@@*                                                              @@@@@@
  @@@@@@@*                                                              @@@@@@
  @@@@@@@#.............................................................*@@@@@@
  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@



  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  @@@@@@@:                                                              @@@@@@
  @@@@@@@*                                                              @@@@@@
  @@@@@@@*                                                              @@@@@@
  @@@@@@@*                                                              @@@@@@
  @@@@@@@*                                                              @@@@@@
  @@@@@@@*           @@                                                 @@@@@@
  @@@@@@@*           @@                      .                          @@@@@@
  @@@@@@@*           @@@@@@*  @@@@@@ @@@@@@  @@@@@@ @@   @@:            @@@@@@
  @@@@@@@*           @@   @@ @@  *@. @@   @@ @@   @* @@.#@8             @@@@@@
  @@@@@@@*           @@   @@ @@   @& @@   @@ @@  #@   @@@@              @@@@@@
  @@@@@@@*           @@   @@  @@@@@@ @@@@@o  @@@@@     @@               @@@@@@
  @@@@@@@*                           @@      @@       @@                @@@@@@
  @@@@@@@*                           :@      @#       @                 @@@@@@
  @@@@@@@*                                                              @@@@@@
  @@@@@@@#.............................................................*@@@@@@
  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@



  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  @@@@@@@:                                                              @@@@@@
  @@@@@@@*                                                              @@@@@@
  @@@@@@@*                                                              @@@@@@
  @@@@@@@*                                                              @@@@@@
  @@@@@@@*       @@          @#               @@            .@o         @@@@@@
  @@@@@@@*       @@         @@@@@@@@@@@       @@       @@@@@@@@@@@@@    @@@@@@
  @@@@@@@*   @@88888888@@  @@    @@     &@@@@@@@@@@@@@      .@o         @@@@@@
  @@@@@@@*   @@@@@@@@@:@.  @@@@@@@@@@@@       @@        @@@@@@@@@@@     @@@@@@
  @@@@@@@*   @@   @@@@@@    @@   @@          @@@@       @@@@@@@@@@@     @@@@@@
  @@@@@@@*  *@o @@@@@@    @@@@@@@@@@@@@@    @@  @@@     @@       @@     @@@@@@
  @@@@@@@*  @@ @@@  @@ @@        @@      @@@@    *@@@8  @@@@@@@@@@@     @@@@@@
  @@@@@@@*     @     *o          @@      .          #   @@       @@     @@@@@@
  @@@@@@@*                                                              @@@@@@
  @@@@@@@*                                                              @@@@@@
  @@@@@@@#.............................................................*@@@@@@
  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@



  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  @@@@@@@:                                                              @@@@@@
  @@@@@@@*                                                              @@@@@@
  @@@@@@@*                                                              @@@@@@
  @@@@@@@*                                                              @@@@@@
  @@@@@@@*       @@             @8             @@              @@ @     @@@@@@
  @@@@@@@*       @@             @8        @@.  @@       @@@@@@@@@@@@    @@@@@@
  @@@@@@@*   @@88888888@@  @@888888888@* @@@@@@@@@@@@.  @      @@       @@@@@@
  @@@@@@@*   @@@@@@@@@:@.  @@#@@@@@@8@8 #@8    @@       @  @8  #@ @@    @@@@@@
  @@@@@@@*   @@   @@@@@@   @@   @@@@@@    *@@@@@@@@@@   @ @@@@@.@@@     @@@@@@
  @@@@@@@*  *@& @@@@@@     @@ :@@@@@.          @@      #@.@@@@ .@@ @    @@@@@@
  @@@@@@@*  @@ @@@  @@ @@ @@*.@@  .@&*@. @@@@@@@@@@@@@ @@:@@@@@@@@&@.   @@@@@@
  @@@@@@@*     @     *o    # :.     o*                        o   @.    @@@@@@
  @@@@@@@*                                                              @@@@@@
  @@@@@@@*                                                              @@@@@@
  @@@@@@@#.............................................................*@@@@@@
  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

  ████   ████   ████   ████    ██
 █░░░ █ █░░░██ █░░░ █ █░░░ █  ░██                ██████  ██████   ██   ██
░    ░█░█  █░█░    ░█░    ░█  ░██       ██████  ░██░░░██░██░░░██ ░░██ ██
   ███ ░█ █ ░█   ███    ███   ░██████  ░░░░░░██ ░██  ░██░██  ░██  ░░███
  █░░  ░██  ░█  █░░    █░░    ░██░░░██  ███████ ░██████ ░██████    ░██
 █     ░█   ░█ █      █       ░██  ░██ ██░░░░██ ░██░░░  ░██░░░     ██
░██████░ ████ ░██████░██████  ░██  ░██░░████████░██     ░██       ██
░░░░░░  ░░░░  ░░░░░░ ░░░░░░   ░░   ░░  ░░░░░░░░ ░░      ░░       ░░

2022-01-10 11:13:36.266  INFO 81288 --- [           main] com.ljw.Application                      : Starting Application using Java 1.8.0_202 on LAPTOP-HTA2F6RV with PID 81288 (E:\project\private\github\java-guide\banner-spring-boot\target\classes started by ljw in E:\project\private\github\java-guide)
2022-01-10 11:13:36.268  INFO 81288 --- [           main] com.ljw.Application                      : No active profile set, falling back to default profiles: default
2022-01-10 11:13:36.572  INFO 81288 --- [           main] com.ljw.Application                      : Started Application in 4.772 seconds (JVM running for 5.362)

Process finished with exit code 0
复制代码

这里同时打印了gif文件和banner.txt文件,可以看出gif打印是按照gif的帧数打印的。

☀️SpringBoot源码分析之Banner

我们来分析下springboot是如何控制banner的打印吧,SpringBoot启动类,点击run进出查看

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
复制代码
public static ConfigurableApplicationContext run(Class<?> primarySource, String... args) {
    return run(new Class[]{primarySource}, args);
}
复制代码

这里new一个class资源类

public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) {
    return (new SpringApplication(primarySources)).run(args);
}
复制代码

new了一个SpringApplication启动类返回了,进入这个构造函数

public SpringApplication(Class<?>... primarySources) {
    this((ResourceLoader)null, primarySources);
}
复制代码

这里还是调用自身的构造函数

public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
    this.sources = new LinkedHashSet();
    //这里启动时指定了banner的模式
    this.bannerMode = Mode.CONSOLE;
    this.logStartupInfo = true;
    this.addCommandLineProperties = true;
    this.addConversionService = true;
    this.headless = true;
    this.registerShutdownHook = true;
    this.additionalProfiles = Collections.emptySet();
    this.isCustomEnvironment = false;
    this.lazyInitialization = false;
    this.applicationContextFactory = ApplicationContextFactory.DEFAULT;
    this.applicationStartup = ApplicationStartup.DEFAULT;
    this.resourceLoader = resourceLoader;
    Assert.notNull(primarySources, "PrimarySources must not be null");
    this.primarySources = new LinkedHashSet(Arrays.asList(primarySources));
    this.webApplicationType = WebApplicationType.deduceFromClasspath();
    this.bootstrapRegistryInitializers = new ArrayList(this.getSpringFactoriesInstances(BootstrapRegistryInitializer.class));
    this.setInitializers(this.getSpringFactoriesInstances(ApplicationContextInitializer.class));
    this.setListeners(this.getSpringFactoriesInstances(ApplicationListener.class));
    this.mainApplicationClass = this.deduceMainApplicationClass();
}
复制代码

该构造函数默认一些属性进行初始化,这里我们可以在启动时调用相应的setter可以重新设置属性,banner的模式初始化,语句:this.bannerMode = Mode.CONSOLE; 我们点进去看看这个枚举类

@FunctionalInterface
public interface Banner {
    void printBanner(Environment environment, Class<?> sourceClass, PrintStream out);

    public static enum Mode {
        OFF,
        CONSOLE,
        LOG;

        private Mode() {
        }
    }
}
复制代码

Banner定义了三种模式,从字面意思就能知道作用

  • OFF : 关闭Banner
  • CONSOLE :控制台输出
  • LOG : 日志输出

我们再回去之前run看看初始化后做了什么

public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) {
    return (new SpringApplication(primarySources)).run(args);
}
复制代码

我们知道调用再new SpringApplication(primarySources)会调用构造函数,初始化一些默认属性,再调用run(args)方法,我们点run进去看看

public ConfigurableApplicationContext run(String... args) {
    long startTime = System.nanoTime();
    DefaultBootstrapContext bootstrapContext = this.createBootstrapContext();
    ConfigurableApplicationContext context = null;
    this.configureHeadlessProperty();
    SpringApplicationRunListeners listeners = this.getRunListeners(args);
    listeners.starting(bootstrapContext, this.mainApplicationClass);

    try {
        ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
        ConfigurableEnvironment environment = this.prepareEnvironment(listeners, bootstrapContext, applicationArguments);
        this.configureIgnoreBeanInfo(environment);
        //这里很明显是打印banner
        Banner printedBanner = this.printBanner(environment);
        context = this.createApplicationContext();
        context.setApplicationStartup(this.applicationStartup);
        this.prepareContext(bootstrapContext, context, environment, listeners, applicationArguments, printedBanner);
        this.refreshContext(context);
        this.afterRefresh(context, applicationArguments);
        Duration timeTakenToStartup = Duration.ofNanos(System.nanoTime() - startTime);
        if (this.logStartupInfo) {
            (new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), timeTakenToStartup);
        }

        listeners.started(context, timeTakenToStartup);
        this.callRunners(context, applicationArguments);
    } catch (Throwable var12) {
        this.handleRunFailure(context, var12, listeners);
        throw new IllegalStateException(var12);
    }

    try {
        Duration timeTakenToReady = Duration.ofNanos(System.nanoTime() - startTime);
        listeners.ready(context, timeTakenToReady);
        return context;
    } catch (Throwable var11) {
        this.handleRunFailure(context, var11, (SpringApplicationRunListeners)null);
        throw new IllegalStateException(var11);
    }
}
复制代码

Banner printedBanner = this.printBanner(environment)语句就是打印banner,我们继续点进去看看

private Banner printBanner(ConfigurableEnvironment environment) {
   //判断是否打印
   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);
   }
}
复制代码

这里就是判断banner输出模式,模式使控制台,这里点击bannerPrinter.print()进去查看

Banner print(Environment environment, Class<?> sourceClass, Log logger) {
    //获取一个Banner
    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);
}
复制代码

这里有一个语句:Banner banner = this.getBanner(environment);根据环境获取banner,点进去看看

private Banner getBanner(Environment environment) {
    SpringApplicationBannerPrinter.Banners banners = new SpringApplicationBannerPrinter.Banners();
    //这里获取一个图片banner
    banners.addIfNotNull(this.getImageBanner(environment));
     //这里获取一个文本banner
    banners.addIfNotNull(this.getTextBanner(environment));
    if (banners.hasAtLeastOneBanner()) {
    //至少有一个banner就返回
        return banners;
    } else {
        //这里直接返回了springboot默认的banner
        return this.fallbackBanner != null ? this.fallbackBanner : DEFAULT_BANNER;
    }
}
复制代码

我们点击获取图片的方法:banners.addIfNotNull(this.getImageBanner(environment));

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()) {
                //我们自定义了配置就会立刻返回,只会加载一个banner
                return new ImageBanner(resource);
            }
        }

        return null;
    }
}
复制代码

这里可以看出是,我们环境配置文件的spring.banner.image.location的参数,就会进行加载,并且只会加载一个banner,我们查看图片类型如下:

static final String[] IMAGE_EXTENSION = new String[]{"gif", "jpg", "png"};
复制代码

可以配置gif动画的图片,并且有顺序,有就返回了。

我们查看文本banner: banners.addIfNotNull(this.getTextBanner(environment));进去查看

private Banner getTextBanner(Environment environment) {
    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;
}
复制代码

也是环境中默认定义了配置key为spring.banner.location,value名称为:banner.txt,只要我们把banner.txt放在classpath下就可以了。

👋 写在最后

  • 首先很感谢大家看到这里,这次的文章就分享到这里,祝大家新春快乐!
  • 👍🏻:有收获的,点赞鼓励!
  • ❤️:收藏文章,方便回看!
  • 💬:评论交流,互相进步!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值