1. 程式人生 > >springboot學習-關閉預設的banner及自定義banner解析

springboot學習-關閉預設的banner及自定義banner解析

sprinboot預設是開啟banner的,如下圖:

如果不想輸出logo,可以通過以下兩種方法來關閉:

1.修改main方法

public class SpringbootstudyApplication {

	public static void main(String[] args) {
		SpringApplication springApplication=new SpringApplication(SpringbootstudyApplication.class);
		springApplication.setBannerMode(Banner.Mode.OFF);
		springApplication.run(args);

	}

2.修改yml檔案

spring:
  main:
    banner-mode: "off"

注意:雙引號一定要加,否則報

 Cannot convert value of type 'java.lang.Boolean' to required type 'org.springframework.boot.Banner$Mode' for property 'bannerMode': no matching editors or conversion strategy found]

預設的banner為什麼會是哪個樣子?在sources的jar包下檢視原始碼可知:

class SpringBootBanner implements Banner {

    //預設的影象
	private static final String[] BANNER = { "",
			"  .   ____          _            __ _ _",
			" /\\\\ / ___'_ __ _ _(_)_ __  __ _ \\ \\ \\ \\",
			"( ( )\\___ | '_ | '_| | '_ \\/ _` | \\ \\ \\ \\",
			" \\\\/  ___)| |_)| | | | | || (_| |  ) ) ) )",
			"  '  |____| .__|_| |_|_| |_\\__, | / / / /",
			" =========|_|==============|___/=/_/_/_/" };

	private static final String SPRING_BOOT = " :: Spring Boot :: ";

	private static final int STRAP_LINE_SIZE = 42;

	@Override
	public void printBanner(Environment environment, Class<?> sourceClass,
			PrintStream printStream) {
        //新增到printStream 中
		for (String line : BANNER) {
			printStream.println(line);
		}
        //獲取springboot的版本
		String version = SpringBootVersion.getVersion();
		version = (version == null ? "" : " (v" + version + ")");
		String padding = "";
		while (padding.length() < STRAP_LINE_SIZE
				- (version.length() + SPRING_BOOT.length())) {
			padding += " ";
		}

		printStream.println(AnsiOutput.toString(AnsiColor.GREEN, SPRING_BOOT,
				AnsiColor.DEFAULT, padding, AnsiStyle.FAINT, version));
		printStream.println();
	}

}

同時檢視SpringApplicationBannerPrinter的原始碼:

    //部分原始碼
	static final String BANNER_LOCATION_PROPERTY = "banner.location";

	static final String BANNER_IMAGE_LOCATION_PROPERTY = "banner.image.location";

	static final String DEFAULT_BANNER_LOCATION = "banner.txt";

	static final String[] IMAGE_EXTENSION = { "gif", "jpg", "png" };
    //呼叫了SpringBootBanner
	private static final Banner DEFAULT_BANNER = new SpringBootBanner();

這樣就可以知道,我們自定義banner時,為什麼需要在source目錄新建banner.txt,以及圖片的格式為gif、jpg、png啦