`

Spring Boot 进阶之Banner

阅读更多
  Spring Boot在启动项目时,控制台会打印一个Springlogo。如果不做任何配置,则会打印出以下信息:

.   ____          _            __ _ _

 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \

( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \

 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )

  '  |____| .__|_| |_|_| |_\__, | / / / /

 =========|_|==============|___/=/_/_/_/

该信息来源于SpringBootBanner类的静态常量BANNER,该属性是一个字符串数组,不指定任何banner属性时,控制台默认输出该数组数据。我们可以通过Spring Boot提供的强大配置功能来改变banner的输出。

1、通过代码设置

  在main方法中创建SpringApplication对象,通过该实例对象设置banner属性,

public static void main(String[] args) {
    SpringApplication app = new SpringApplication(SpringBootSimpleApplication.class);
    app.setBanner(new Banner() {
        @Override
	public void printBanner(Environment environment, Class<?> sourceClass, PrintStream out) {
	    out.print("\n\n\tSpring Boot Sample App!\n\n".toUpperCase());
	}
    });
    app.run(args);
}

运行该main方法,控制台将打印出自定义的banner信息:

 



    SPRING BOOT SAMPLE APP!

2017-04-06 17:10:14.954  INFO 9808 --- [           main] the.spring.SpringBootSimpleApplication

……

2、通过定义文本文件

  可以在http://patorjk.com网站的“Text to ASCII Art Generator”中生成个性化的文本作为项目的banner,并保存在banner.txt文件中,然后将banner.txt文件放在项目的src/main/resources目录下,项目启动时Spring Boot会加载该路径下的banner文件,另外该属性文件会覆盖main方法中的设置,效果如下:

 ___          _             ___           _
/ __|_ __ _ _(_)_ _  __ _  | _ ) ___  ___| |_
\__ \ '_ \ '_| | ' \/ _` | | _ \/ _ \/ _ \  _|
|___/ .__/_| |_|_||_\__, | |___/\___/\___/\__|
    |_|             |___/

2017-04-06 17:29:26.012  INFO 1668 --- [           main] the.spring.SpringBootSimpleApplication

……

Spring Boot默认加载resources目录下的banner.txt(默认文件名)文件,运行时也可以通过设置banner.location属性值来加载banner文件:--banner.location=classpath:/META-INF/banner.txt,同样可以在application.properties文件中设置banner.location=classpath:/META-INF/banner.txt,利用属性值配置时,banner.txt的名字可以自定义

3、关闭banner显示

在程序中可以使用app.setBannerMode(Mode.OFF);来关闭banner,利用属性值设置则为:spring.main.banner-mode=off

 

0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics