SpringBoot之Banner
博客专区 > Mr_Qi 的博客 > 博客详情
SpringBoot之Banner
Mr_Qi 发表于1周前
SpringBoot之Banner
  • 发表于 1周前
  • 阅读 324
  • 收藏 18
  • 点赞 0
  • 评论 0

背景

初次运行SpringBoot的小伙伴想必对于SpringBoot打印的Banner很感兴趣

通常长成这样

一个Spring扑面而来~

那么我们能否定制自己的启动页呢?

源码

SpringBoot在启动时会调用如下一段代码 

private Banner printBanner(ConfigurableEnvironment environment) {
   if (this.bannerMode == Banner.Mode.OFF) {
      return null;
   }
   ResourceLoader resourceLoader = this.resourceLoader != null ? this.resourceLoader
         : new DefaultResourceLoader(getClassLoader());
   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);
}
public Banner print(Environment environment, Class<?> sourceClass, PrintStream out) {
   Banner banner = getBanner(environment, this.fallbackBanner);
   banner.printBanner(environment, sourceClass, out);
   return new PrintedBanner(banner, sourceClass);
}
private Banner getBanner(Environment environment, Banner definedBanner) {
   Banners banners = new Banners();
   banners.addIfNotNull(getImageBanner(environment));
   banners.addIfNotNull(getTextBanner(environment));
   if (banners.hasAtLeastOneBanner()) {
      return banners;
   }
   if (this.fallbackBanner != null) {
      return this.fallbackBanner;
   }
   return DEFAULT_BANNER;
}
private static final Banner DEFAULT_BANNER = new SpringBootBanner();

 

而 SpringBootBanner长成这样

/**
 * Default Banner implementation which writes the 'Spring' banner.
 *
 * @author Phillip Webb
 */
class SpringBootBanner implements Banner {
 
   private static final String[] BANNER = { "",
         "  .   ____          _            __ _ _",
         " /\\\\ / ___'_ __ _ _(_)_ __  __ _ \\ \\ \\ \\",
         "( ( )\\___ | '_ | '_| | '_ \\/ _` | \\ \\ \\ \\",
         " \\\\/  ___)| |_)| | | | | || (_| |  ) ) ) )",
         "  '  |____| .__|_| |_|_| |_\\__, | / / / /",
         " =========|_|==============|___/=/_/_/_/" };
 
   private static final String SPRING_BOOT = " :: Spring Boot :: ";
 
   private static final int STRAP_LINE_SIZE = 42;
 
   @Override
   public void printBanner(Environment environment, Class<?> sourceClass,
         PrintStream printStream) {
      for (String line : BANNER) {
         printStream.println(line);
      }
      String version = SpringBootVersion.getVersion();
      version = (version == null ? "" : " (v" + version + ")");
      String padding = "";
      while (padding.length() < STRAP_LINE_SIZE
            - (version.length() + SPRING_BOOT.length())) {
         padding += " ";
      }
 
      printStream.println(AnsiOutput.toString(AnsiColor.GREEN, SPRING_BOOT,
            AnsiColor.DEFAULT, padding, AnsiStyle.FAINT, version));
      printStream.println();
   }
 
}

上述代码可以看到banner存在两种 imageBanner和textBanner

imageBanner需要配置属性为banner.image.location

或者支持名为banner格式为 "gif", "jpg", "png的图片 

textBanner支持默认情况下在banner.location 或者默认为banner.txt

比如我们可以如下设置

在resource分别放置banner.txt和banner.png

banner.png

banner.txt

/$$$$$$$$ /$$$$$$
| $$_____//$$__  $$
| $$     | $$  \__/
| $$$$$  | $$$$$$$
| $$__/  | $$__  $$
| $$     | $$  \ $$
| $$     |  $$$$$$/
|__/      \______/

我们可以看一下结果

哈哈 F6小人变成了这样!!!

生成文字banner可以使用 http://patorjk.com/software/taag/#p=display&f=Big%20Money-ne&t=F6 【同样可以用在javascript 打印出console~】

 

标签: SpringBoot banner
共有 人打赏支持
粉丝 170
博文 178
码字总数 186551
×
Mr_Qi
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: