亲宝软件园·资讯

展开

SpringBoot之Banner的使用示例

人气:0

背景

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

Spring Boot在启动项目时,控制台会打印一个Spring的logo。如果不做任何配置

该信息来源于SpringBootBanner类的静态常量BANNER,该属性是一个字符串数组,不指定任何banner属性时,控制台默认输出该数组数据。我们可以通过Spring Boot提供的强大配置功能来改变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显示

在程序中可以使用app.setBannerMode(Mode.OFF);来关闭banner,利用属性值设置则为:spring.main.banner-mode=off

您可能感兴趣的文章:

加载全部内容

相关教程
猜你喜欢
用户评论