springboot学习-关闭默认的banner及自定义banner解析

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/nanruitao10/article/details/85624686

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啦

猜你喜欢

转载自blog.csdn.net/nanruitao10/article/details/85624686