玩转Spring Boot 自定义Banner
到这里相信大家应该都见过以下图案。
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.4.2.RELEASE)
该图案是在启动Spring Boot的时候在控制台输出的,包含当前使用的Spring Boot的版本,有的时候需要定制自己的Banner,也可能关闭输出Banner。接下来介绍如何更改。
1.关闭Banner
在启动的时候调用setBannerMode设置即可,代码如下:
public static void main(String[] args) throws Exception {
SpringApplication springApplication = new SpringApplication(SampleController.class);
springApplication.setBannerMode(Banner.Mode.OFF);
springApplication.run(args);
}
Banner.Mode是个内部枚举,有三个值如下:
enum Mode {
/**
* Disable printing of the banner. 关闭Banner打印输出
*/
OFF,
/**
* Print the banner to System.out. 打印Banner 控制台
*/
CONSOLE,
/**
* Print the banner to the log file. 打印banner 到日志文件
*/
LOG
}
2.更改Banner
(1)在src/main/resources下新建banner.txt,在该文件中输入你想要的图案即可 。在看《JavaEE开发的颠覆者 Spring Boot实战》一书的时候,作者在书中写了一个网站,可以生成相应的图案,具体大家去试试,网址:
http://patorjk.com/software/taag。在banner.txt文件中,你可以使用以下的占位符获取当前应用版本等信息,具体如下:
Variable | Description |
${application.version} | The version number of your application as declared in MANIFEST.MF formatted for display (surrounded with brackets and prefixed with v). For example (v1.0). |
${application.formatted-version} | The version number of your application as declared in MANIFEST.MF formatted for display (surrounded with brackets and prefixed with v). For example (v1.0) |
${spring-boot.version} | The Spring Boot version that you are using. For example 1.4.2.RELEASE. |
${spring-boot.formatted-version} | The Spring Boot version that you are using formatted for display (surrounded with brackets and prefixed with v). For example (v1.4.2.RELEASE). |
${Ansi.NAME} (or ${AnsiColor.NAME}, ${AnsiBackground.NAME}, ${AnsiStyle.NAME}) | Where NAME is the name of an ANSI escape code. See AnsiPropertySource for details. |
${application.title} | The title of your application as declared in MANIFEST.MF. For example Implementation-Title: MyApp is printed as MyApp. |
(2)或者通过springApplication.setBanner(banner);方法,实现Banner接口也一样。
有兴趣的朋友可以加群探讨相互学习:
Spring Boot QQ交流群:599546061