Make your SpringBoot more personalized, customize cool Banner

foreword

When developing SpringCloud, I wonder if my friends have noticed that there will be a startup screen in the log when your SpringBoot microservice starts; as shown in the figure below

It is very cool and stylish; this method can be realized very simply through the SPI method provided in SpringBoot; what we want to introduce to you today is to display such an effect through a few lines of code, although there is not much technology here Content, but it can bring more personality to our SpringCloud or SpringBoot microservices, and our self-developed Starter; many customized starters developed by the author use this method to increase personality; at the same time, we pass this article. SpringBoot's SPI mechanism and extensions will be used as a starting point; it will bring out more in-depth views on SpringBoot SPI in the future.

SpringBoot SPI

The method we introduce here today is implemented through the SPI mechanism provided by SpringBoot; the SPI mechanism of SpringBoot is a reference to the SPI provided by Spring Framework Core; before SpringBoot, Spring Framework has provided SPI Mechanism, SPI students need to understand, Java has SPI, Dubbo has SPI, a way to realize IOC; Spring Framework provides two ways of Handler and Factory, for spring.handlers and spring under Meta-INFo .factories two files, the former is for the Spring Configuraiton Loader in the xml mode of the Spring Framework, and the latter spring.factories is the Loader loading method of the Bean AutoConfiguration that is widely used in Spring Boot. Today we use the latter.

Custom Banner class

Create a custom BannerInitializer class to achieve the cool effect in the above picture


/**
 * @program: https://www.joinsunsoft.com
 * @description:
 * @author: LiuYong
 * @create: 2021-09-20 13:38
 */

public class BannerInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
    public BannerInitializer(){
    }

    private String logPath = "/logo/slant.txt";
    private String product = "Central Platform";

    @Override
    public void initialize(ConfigurableApplicationContext applicationContext) {
        if (!(applicationContext instanceof AnnotationConfigApplicationContext)) {
            LogoBanner logoBanner = new LogoBanner(BannerInitializer.class, this.logPath, "Welcome to joinsunsoft.", 1, 6, new Color[5], true);
            CustomBanner.show(logoBanner, new Description(BannerConstant.VERSION + ":", CommonConstant.PROJECT_VERSION, 0, 1)
                    , new Description("Product:", this.product, 0, 1)
                    , new Description("WebSite:", "https://www.ginghan.com", 0, 1)
                    , new Description("Vendor:", "https://www.joinsunsoft.com", 0, 1)
            );
        }
    }
}

In the above code, logPath is the file corresponding to the graphics in the Banner; the initialize method corresponds to the version description information under the Banner;

Banner file

The online "Text Art Word Online Tool" can convert text into text images, such as jionsunsoft can convert to, and the file corresponds to the path in the previous "private String logPath = "/logo/slant.txt";"

      _      _                             _____
     (_)__  (_)__  ___ __ _____  ___ ___  / _/ /_
    / / _ \/ / _ \(_-</ // / _ \(_-</ _ \/ _/ __/
 __/ /\___/_/_//_/___/\_,_/_//_/___/\___/_/ \__/
|___/ :: Central Platform ::   (v4.6.0.RELEASE)

spring.factories file

In the spring.factories file under meta-info, add the automatic loading information of the BannerInitializer class.

org.springframework.context.ApplicationContextInitializer=\
com.joinsun.springcloud.core.banner.BannerInitializer

you're done

Now start our SpringApplication entry class, and you can see our top effect; SpringBoot provides some information about banner color, background color and version number. AnsiColor and AnsiBackground provide a variety of colors for developers to choose; at the same time It also supports other formats other than text, such as image formats (jpeg/png, etc.); I haven't studied this in depth, and friends who are really interested can study it by themselves and share it;

conclusion

The creation content of this article has no value or significance to our business development. What the author writes is purely to inspire friends to have more relaxed and interesting things in the boring code research. Although the Banner I saw doesn’t have much technical content, it involves SpringBoot and Spring’s SPI-related applications. This is a topic that can be in-depth; Leave a message; want to know what related technical points; we will further explore with text. I hope everyone will actively leave a message; continue to pay attention;

Thank you for your attention;

Guess you like

Origin blog.csdn.net/inthirties/article/details/126808800