Custom Banner for SpringBoot Source Analysis

1. What is Banner

When we start a SpringBoot application, we often see the following print in the console

  .   ____          _            __ _ _

 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \

( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \

 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )

  '  |____| .__|_| |_|_| |_\__, | / / / /

 =========|_|==============|___/=/_/_/_/

 :: Spring Boot ::        (v2.1.6.RELEASE)

The output is Banner, which looks like a logo for a project.

Second, how to customize Banner

What should I do if I want the banner of my project to print the patterns I want: such as company LOGO, etc.?

It's very simple, just create a file named banner.txt in the /src/main/resources directory of the SpringBoot project , so that the container will print the text in this file on the console when it starts.

So how to draw some patterns? There are many ready-made websites that support banner design, you can refer to

1.http://patorjk.com/software/taag

Enter text to convert

2.http://www.network-science.de/ascii/

Enter text to convert

3.http://www.degraeve.com/img2txt.php

Can convert a picture into a text format picture

image

image

Three, source code analysis

So how is SpringBoot implemented?

In the run method of SpringApplication, there is a line of Banner printedBanner = printBanner(environment); Get and print the banner in this method.

SpringApplication.printBanner

private Banner printBanner(ConfigurableEnvironment environment) {

    检查打印Banner的开关是否启

 if (this.bannerMode == Banner.Mode.OFF) {

 return null;

 }

 ResourceLoader resourceLoader = (this.resourceLoader != null) ? this.resourceLoader

   : new DefaultResourceLoader(getClassLoader());

 //准备printer

 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);

}

There are three Banner.Mode:

  • OFF No banner output in any form

  • CONSOLE Print the banner on the console

  • LOG Print Banner to log file

The default Banner.Mode type in SpringApplication is CONSOLE.

ResourceLoader is a strategy interface provided by Spring for obtaining resources under the classpath or resource directory. After getting the ResourceLoader, package it as SpringApplicationBannerPrinter. Then call its print method.

SpringApplicationBannerPrinter.print

public Banner print(Environment environment, Class<?> sourceClass, PrintStream out) {

    //获取banner

 Banner banner = getBanner(environment);

 //打印banner

 banner.printBanner(environment, sourceClass, out);

 return new PrintedBanner(banner, sourceClass);

}

SpringApplicationBannerPrinter.getBanner

private Banner getBanner(Environment environment) {

 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;

}

From the getBanner code, it not only supports banners in txt format, but also supports banners of image resources. Looking at the implementation of the getImageBanner(environment) method, you can see that SpringBoot will obtain the image path from the spring.banner.image.location property, support gif, jpg, and png, and then encapsulate it as ImageBanerr. Among them, Banners essentially maintain a list of banners. Iteratively prints when actually printing.

SpringApplicationBannerPrinter.printBanner

 public void printBanner(Environment environment, Class<?> sourceClass, PrintStream out) {

 for (Banner banner : this.banners) {

   banner.printBanner(environment, sourceClass, out);

 }

 }

All springboots also support printing of multiple banners.

Four, source code summary

We can learn more about banner support from the source code, which will be listed below

  • To customize the banner by default, you only need to place a banner.txt file in the resource directory. If you don’t want to name it banner.txt, you can specify the attribute spring.banner.location as the custom file name.

  • Support image resource banner. Specify the property spring.banner.image.location as the image resource path

  • SpringBoot supports configuring txt format and image format banner at the same time, and printing at the same time.

Guess you like

Origin blog.csdn.net/qq_46388795/article/details/109049278