preface

In the previous article, the Spring Boot project was built and started successfully. When the project starts, the following information is printed in the console:

Why is the Banner printed

Check the SpringApplicationBannerPrinter class getBanner method is as follows:

SpringBoot first gets custom image Banner and text Banner, if not custom, use the default Banner, namely SpringBootBanner.

See the SpringBootBanner class BANNER constant, you can pat the thigh and say: it is written dead in code ah! Its printBanner method is the actual printing method.

Looking at the source code, we found that we can customize the image Banner and text Banner, the actual operation below.

Custom picture Banner

It can be in GIF, JPG, or PNG format. Spring Boot will parse the image pixel and output it to the console in ANSII mode.

For example, I found the weibo icon online and put it in the SRC /resources folder called banner.png

After the program starts, you can see the console print:

Image banners support several configurations

  • Spring, the banner image. The location specified banner image location
  • Spring, the banner image. The width specified print width of the banner
  • Spring. Banner. Image. The height specified print banner level

Custom text Banner

TXT file, also in SRC /resources. You can use an online site such as www.bootschool.net/ascii-art for documentation

Here the following text is used:

${AnsiColor.BRIGHT_RED} _ _ooOoo_ o8888888o 88" . "88 (| -_- |) O\ = /O ____/`---'\____ .' \\| |// `. / \\||| : | | | / / / / _ | | | | | - : - | | | | | _ \ | | \ \ \ - / '| | | | \ _ | ` \ ` -' / / | _ | \. - \ __ ` - - '__ / -. / ___ `..' / -. - \ `.. '___ . "" '< `. ___ \ _ < | > _ / ___.' _ > \" "| | : ` - \ `.; `. _ /; . '/ /'; | \ \ `-. \_\_`. _.'_/_/ -' _.' / ================-.`___`-.__\ \___ /__.-'_.'_.-'================ `=--=-' Spring Boot Version: ${spring-boot.version}Copy the code

After the program starts, you can see the console print:

You can use placeholders in the text

  • ${ansicolor.bright_red} Specifies the color of output content in the console
  • ${application.version} Specifies the version number of the application
  • ${application. Formatted -version} The version number of the application (with the prefix V, such as v1.0)
  • ${spring-boot.version} Specifies the spring boot version number
  • ${spring-boot.formatted-version} The version number of spring boot (with the prefix V, for example, v1.0).
  • ${application.title} Specifies the title of the application

Text banners support several configurations

  • Spring.banner. Location Specifies the banner text location
  • Spring.banner. Charset sets the file encoding, default utF-8

Close the Banner

If you do not want to display the Banner, you can write:

@SpringBootApplication
public class FrameworkApplication {

    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(FrameworkApplication.class);
        / / close the Bannerapplication.setBannerMode(Banner.Mode.OFF); application.run(args); }}Copy the code

This can also be turned off by setting spring.main.banner-mode=off

conclusion

In fact, custom banners don’t really work. But it can add a little bit of interest, just to relieve the boredom.

The source code

Github.com/zhuqianchan…

Review past

  • Build backend frameworks from scratch – keep updating