What is Banner

When we launch a SpringBoot application, we often see the following print on the console

  .   ____          _            __ _ _



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



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



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



'|
____| | 4_| |_| | - | | _ \ __ / / / /



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



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

Copy the code

The output is called the Banner, which looks like the logo of a project.

How to customize the Banner

If you want the banner of your project to print the pattern you want, such as the company LOGO, what should I do?

Simply create a file called banner. TXT in the/SRC /main/resources directory of the SpringBoot project, and the container will print the text from this file to the console at startup.

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

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

Input text for conversion

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

Input text for conversion

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

You can turn a picture into a text format picture

image

Three, source code analysis

So how is SpringBoot implemented?

PrintedBanner = printBanner(environment); It is in this method that the banner is fetched and printed.

SpringApplication.printBanner

private Banner printBanner(ConfigurableEnvironment environment) {



Check whether the switch of the print Banner is enabled



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



 return null;



 }



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



   : new DefaultResourceLoader(getClassLoader());



 / / ready to printer



 SpringApplicationBannerPrinter bannerPrinter = new SpringApplicationBannerPrinter(resourceLoader, this.banner);



 // Print in the log



 if (this.bannerMode == Mode.LOG) {



 return bannerPrinter.print(environment, this.mainApplicationClass, logger);



 }



 // Print in the console



 return bannerPrinter.print(environment, this.mainApplicationClass, System.out);



}

Copy the code

There are three types of banner. Mode:

  • OFF does not do any kind of Banner output

  • CONSOLE prints the Banner from the CONSOLE

  • LOG Prints the Banner to the LOG file

The default Banner.Mode type in SpringApplication is CONSOLE.

ResourceLoader is a policy interface provided by Spring for obtaining resources in the CLASspath or resource directory. After get the ResourceLoader encapsulation for SpringApplicationBannerPrinter them. Then call its print method.

SpringApplicationBannerPrinter.print

public Banner print(Environment environment, Class<? > sourceClass, PrintStreamout{



    / / get the banner



 Banner banner = getBanner(environment);



 / / print the banner



 banner.printBanner(environment, sourceClass, out);



 return new PrintedBanner(banner, sourceClass);



}

Copy the code

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;



}

Copy the code

From the getBanner code, not only support the TXT form of the banner, but also support the image resource banner. See getImageBanner (environment) method implementation, knowable SpringBoot from spring. Banner. The image. The image path in the location attribute, support GIF, JPG, PNG, and then encapsulated in the ImageBanerr. A clearly Banners is a list of Banners that you maintain. When you actually print, you print through it.

SpringApplicationBannerPrinter.printBanner

 public void printBanner(Environment environment, Class<? > sourceClass, PrintStreamout{



 for (Banner banner : this.banners) {



   banner.printBanner(environment, sourceClass, out);



 }



 }

Copy the code

All SpringBoot also supports the printing of multiple banners.

Four, source summary

We can learn more about banner support from the source code, which is described below

  • By default, you only need to place a banner. TXT file in the resource directory. If you do not want to name the banner

  • Support image resource banner. Specify attributes spring. Banner. Image. The location path for image resources

  • SpringBoot supports both TXT and image banner configuration and printing.