[Basic] SpringBoot Custom Banner

write first

When we start the Spring Boot project, the following will be printed on the console (logo and version information):

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.6.6)
复制代码

hands-on replacement

Generate Banner

You can generate the ASCII art word you want for your project through the following link

Generate banner characters as follows

                          To My Love!
  -=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-

               __        __        __        __
 .*.         /~ .~\    /~  ~\    /~ .~\    /~  ~\
 ***        '      `\/'      *  '      `\/'      *
  V        (                .*)(               . *)
/\|/\       \     Ming   . *./  \      Yue   . *./
  |          `\ .      . .*/'    `\ .      . .*/'     .*.
  |            `\ * .*. */' _    _ `\ * .*. */'       ***
                 `\ * */'  ( `\/'*)  `\ * */'          V
                   `\/'     \   */'    `\/'          /\|/\
                             `\/'                      |

     --+++==##<<{{******** Ming Yue ********}}>>##==++--
复制代码

Replace Banner

Create a banner.txtfile . For example: bannerreplace

image-20220411094548689

Start the project again and print the banner characters as above. At this point, we found that some version information of the corresponding Spring Boot is missing. If you still want it, you can add it.

Append Spring Boot version information, etc.

  1. ${spring-boot.version}: Spring Boot version number;
  2. ${spring-boot.formatted-version}: The formatted version number of Spring Boot.
  3. ${application.version}: the version number in the MANIFEST.MF file;
  4. ${application.formatted-version}: version number information in the formatted MANIFEST.MF file;

Append the variables defined application.propertiesin

app.author=Strive
复制代码

The final file is as follows, start the project again to view and print

                          To My Love!
  -=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-

               __        __        __        __
 .*.         /~ .~\    /~  ~\    /~ .~\    /~  ~\
 ***        '      `\/'      *  '      `\/'      *
  V        (                .*)(               . *)
/\|/\       \     Ming   . *./  \      Yue   . *./
  |          `\ .      . .*/'    `\ .      . .*/'     .*.
  |            `\ * .*. */' _    _ `\ * .*. */'       ***
                 `\ * */'  ( `\/'*)  `\ * */'          V
                   `\/'     \   */'    `\/'          /\|/\
                             `\/'                      |

     --+++==##<<{{******** Ming Yue ********}}>>##==++--
:: Spring Boot :: (${spring-boot.version})
                                           --by ${app.author}
复制代码

It prints as follows:

                          To My Love!
  -=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-

               __        __        __        __
 .*.         /~ .~\    /~  ~\    /~ .~\    /~  ~\
 ***        '      `\/'      *  '      `\/'      *
  V        (                .*)(               . *)
/\|/\       \     Ming   . *./  \      Yue   . *./
  |          `\ .      . .*/'    `\ .      . .*/'     .*.
  |            `\ * .*. */' _    _ `\ * .*. */'       ***
                 `\ * */'  ( `\/'*)  `\ * */'          V
                   `\/'     \   */'    `\/'          /\|/\
                             `\/'                      |

     --+++==##<<{{******** Ming Yue ********}}>>##==++--
:: Spring Boot :: (2.6.6)
                                           --by Strive
复制代码

Implementation principle

BannerThe interface specifically implements this operation. To customize the printing banner, just customize a class to implement this interface and override the printBannermethod to print. When the Springboot project starts, our implementation class object is created and the printBannermethod .

package org.springframework.boot;

import java.io.PrintStream;
import org.springframework.core.env.Environment;

@FunctionalInterface
public interface Banner {
  void printBanner(Environment environment, Class<?> sourceClass, PrintStream out);

  public static enum Mode {
    OFF, // 关闭 banner 打印
    CONSOLE, // 打印 banner 到 控制台
    LOG; // 打印 banner 到日志文件

    private Mode() {
    }
  }
}
复制代码

printBannerThere are three implementations by default:

  • ImageBanner: Load and print image banner;
  • ResourceBanner: Load and print character banner;
  • SpringBootBanner: By default, the SpringBootBanner implementation class is used to print the banner;

image-20220411101213577

Guess you like

Origin juejin.im/post/7085167880781692958