Spring Boot 自定義啟動圖案


Spring Boot在啟動的時候會打印一個默認的SpringBoot的圖案

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.0.1.RELEASE)

打印圖案的行為由SpringApplicationBannerPrinter控制,要打印的圖案通過getBanner()獲取Banner並打印。

  1. 首先通過文件控制圖案,檢查是否存在banner文件,存在則構建ImageBanner和ResourceBanner去打印
  2. 其次通過自定義的Banner 實現類控制圖案,注入fallbackBanner進行打印。
  3. 最后使用默認的SpringBootBanner打印默認圖案

private Banner getBanner(Environment environment, Banner definedBanner) {
    //1、文件控制圖案
	Banners banners = new Banners();
	banners.addIfNotNull(getImageBanner(environment));
	banners.addIfNotNull(getTextBanner(environment));
	if (banners.hasAtLeastOneBanner()) {
		return banners;
	}
    //2.自定義的Banner 實現類控制圖案
	if (this.fallbackBanner != null) {
		return this.fallbackBanner;
	}
    //默認的SpringBootBanner
	return DEFAULT_BANNER;
}

private static class Banners implements Banner {

	private final List<Banner> banners = new ArrayList<Banner>();

	public void addIfNotNull(Banner banner) {
		if (banner != null) {
			this.banners.add(banner);
		}
	}

	public boolean hasAtLeastOneBanner() {
		return !this.banners.isEmpty();
	}

	@Override
	public void printBanner(Environment environment, Class<?> sourceClass,
							PrintStream out) {
		for (Banner banner : this.banners) {
			banner.printBanner(environment, sourceClass, out);
		}
	}

}

文件控制圖案

通過文件控制圖案的方式里,會到配置文件 application.proeprties 里根據spring.banner.locationspring.banner.image.location找到文件和圖片文檔地址,如果沒有就到classpath目錄下找默認的banner.txt和banner.gif/banner.jpg/banner.png,默認文件也沒有就返回null不采用文件內容控制,圖片會根據顏色深度轉成字符圖案。

/* SpringApplicationBannerPrinter.java
 */
static final String BANNER_LOCATION_PROPERTY = "banner.location";
static final String DEFAULT_BANNER_LOCATION = "banner.txt";
private Banner getTextBanner(Environment environment) {
	String location = environment.getProperty(BANNER_LOCATION_PROPERTY,
											  DEFAULT_BANNER_LOCATION);
	Resource resource = this.resourceLoader.getResource(location);
	if (resource.exists()) {
		return new ResourceBanner(resource);
	}
	return null;
}

static final String BANNER_IMAGE_LOCATION_PROPERTY = "banner.image.location";
static final String[] IMAGE_EXTENSION = { "gif", "jpg", "png" };
private Banner getImageBanner(Environment environment) {
	String location = environment.getProperty(BANNER_IMAGE_LOCATION_PROPERTY);
	if (StringUtils.hasLength(location)) {
		Resource resource = this.resourceLoader.getResource(location);
		return (resource.exists() ? new ImageBanner(resource) : null);
	}
	for (String ext : IMAGE_EXTENSION) {
		Resource resource = this.resourceLoader.getResource("banner." + ext);
		if (resource.exists()) {
			return new ImageBanner(resource);
		}
	}
	return null;
}

自定義Banner

定制化 ASCII 字符:http://network-science.de/ascii/
定制化 ASCII 圖片:https://www.degraeve.com/img2txt.php

新建類實現Banner並實現printBanner方法

import java.io.PrintStream;

import org.springframework.boot.Banner;
import org.springframework.core.env.Environment;

public class HelloBanner implements Banner
{

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

        String banner = "##     ## ######## ##       ##        #######  \r\n"
                + "##     ## ##       ##       ##       ##     ## \r\n"
                + "##     ## ##       ##       ##       ##     ## \r\n"
                + "######### ######   ##       ##       ##     ## \r\n"
                + "##     ## ##       ##       ##       ##     ## \r\n"
                + "##     ## ##       ##       ##       ##     ## \r\n"
                + "##     ## ######## ######## ########  #######  ";

        out.println(banner);
    }
}

SpringApplication設置banner

@SpringBootApplication
public class Application
{
    public static void main(String[] args) {
        SpringApplication springApplication = new SpringApplication(Application.class);
        springApplication.setBanner(new MyBanner());
        springApplication.run(args);
    }
}

關閉Banner

Banner有三種打印模式,默認是CONSOLE打印到控制台。

public interface Banner {
	enum Mode {
		OFF,
		CONSOLE,
		LOG
	}
}

如果要關閉打印圖案,在SpringBootApplication中后綴.bannerMode(Banner.Mode.OFF)

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        new SpringApplicationBuilder(Application.class).bannerMode(Banner.Mode.OFF)
                .run(args);
    }

}

也可以在配置文件中配置spring.main.banner-mode=off關閉


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM