0
点赞
收藏
分享

微信扫一扫

【SpringBoot系列】三、SpringBoot特性_SpringApplication类(自定义Banner)


    SpringApplication类作为SpringBoot最基本、最核心的类,在main方法中用来启动SpringBoot项目。一般情况下,只需在main方法中使用SpringApplication.run静态方法来启动项目:

package com.xcbeyond.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
 * SpringBoot启动类
 * @author xcbeyond
 * 2018年7月2日下午5:41:45
 */
@SpringBootApplication
public class SpringbootApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringbootApplication.class, args);
	}
}

关于SpringApplication有以下常用特性:

自定义启动日志logo。(自定义Banner)

    SpringBoot项目默认启动时,日志中会输出如下SpringBoot的日志logo及其版本信息:

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

    在具体项目中,若想订制化属于自己的启动logo,当然也是可以的哟

    其实非常简单,只需在SpringBoot项目的src/main/resources/目录下新建一个banner.txt,然后将ASCII字符画复制进去,启动项目就能替换默认的logo了。例如如下banner.txt内容:

${AnsiColor.BRIGHT_GREEN}
 
                        _..._                                                   .-'''-.                             
                     .-'_..._''.                                               '   _    \             _______       
                   .' .'      '.\/|              __.....__                   /   /` '.   \    _..._   \  ___ `'.    
                  / .'           ||          .-''         '. .-.          .-.   |     \  '  .'     '.  ' |--.\  \   
                 . '             ||         /     .-''"'-.  `.\ \        / /|   '      |  '.   .-.   . | |    \  '  
   ____     _____| |             ||  __    /     /________\   \\ \      / / \    \     / / |  '   '  | | |     |  ' 
  `.   \  .'    /| |             ||/'__ '. |                  | \ \    / /   `.   ` ..' /  |  |   |  | | |     |  | 
    `.  `'    .' . '             |:/`  '. '\    .-------------'  \ \  / /       '-...-'`   |  |   |  | | |     ' .' 
      '.    .'    \ '.          .||     | | \    '-.____...---.   \ `  /                   |  |   |  | | |___.' /'  
      .'     `.    '. `._____.-'/||\    / '  `.             .'     \  /                    |  |   |  |/_______.'/   
    .'  .'`.   `.    `-.______ / |/\'..' /     `''-...... -'       / /                     |  |   |  |\_______|/    
  .'   /    `.   `.           `  '  `'-'`                      |`-' /                      |  |   |  |              
 '----'       '----'                                            '..'                       '--'   '--'              
 
${AnsiColor.BRIGHT_RED}
Application Version: ${application.version}${application.formatted-version}
Spring Boot Version: ${spring-boot.version}${spring-boot.formatted-version}

启动后的效果如下:

【SpringBoot系列】三、SpringBoot特性_SpringApplication类(自定义Banner)_版本号

关于banner.txt中可以使用如下占位符:

变量

描述

${application.version}

MANIFEST.MF中声明的应用版本号,例如Implementation-Version: 1.0会打印1.0

${application.formatted-version}

MANIFEST.MF中声明的被格式化后的应用版本号(被括号包裹且以v作为前缀),用于显示,例如(v1.0)

${spring-boot.version}

当前Spring Boot的版本号,例如1.4.1.RELEASE

${spring-boot.formatted-version}

当前Spring Boot被格式化后的版本号(被括号包裹且以v作为前缀), 用于显示,例如(v1.4.1.RELEASE)

${Ansi.NAME}(或${AnsiColor.NAME},${AnsiBackground.NAME}, ${AnsiStyle.NAME})

NAME代表一种ANSI编码,具体详情查看AnsiPropertySource

${application.title}

MANIFEST.MF中声明的应用title,例如Implementation-Title: MyApp会打印MyApp

ASCII字符画生成工具:

如果让我们手动编辑这些字符画,显然是非常麻烦的一件事,可借助如下工具完成:

  • http://patorjk.com/software/taag
  • http://www.network-science.de/ascii/
  • http://www.degraeve.com/img2txt.php

【SpringBoot系列】三、SpringBoot特性_SpringApplication类(自定义Banner)_main方法_02







举报

相关推荐

0 条评论