一 官方文档介绍

1 自定义横幅

通过在 classpath 中添加banner.txt文件或将banner.location设置为此类文件的位置,可以更改启动时打印的横幅。如果文件具有异常编码,则可以设置banner.charset(默认为UTF-8)。除了文本文件,您还可以将banner.gifbanner.jpgbanner.png图像文件添加到 classpath,或设置banner.image.location property。图像将转换为 ASCII 艺术表示并打印在任何文本横幅上方。

banner.txt文件中,您可以使用以下任何占位符:

表格 1_.横幅变量

变量 描述
${application.version} MANIFEST.MF中声明的 application 的 version number。对于 example Implementation-Version: 1.0打印为1.0
${application.formatted-version} MANIFEST.MF中声明的 application 的 version number 用于显示(用括号括起来并以v为前缀)。对于 example (v1.0)
${spring-boot.version} 您正在使用的 Spring Boot version。对于 example 1.5.9.RELEASE
${spring-boot.formatted-version} 正在使用的 Spring Boot version 格式化显示(用括号括起来并以v为前缀)。对于 example (v1.5.9.RELEASE)
${Ansi.NAME}(或${AnsiColor.NAME}${AnsiBackground.NAME}${AnsiStyle.NAME}) 其中NAME是 ANSI 转义 code 的 name。有关详细信息,请参阅AnsiPropertySource
${application.title} MANIFEST.MF中声明的 application 的标题。对于 example Implementation-Title: MyApp打印为MyApp

如果要以编程方式生成横幅,可以使用SpringApplication.setBanner(…)方法。使用org.springframework.boot.Banner接口并实现自己的printBanner()方法。

您还可以使用spring.main.banner-mode property 来确定是否必须使用已配置的 logger(log)或根本不使用(off)在System.out(console)上打印横幅。

打印的横幅将在 name springBootBanner下注册为 singleton bean。

YAML maps offfalse所以如果要在 application 中禁用横幅,请务必添加引号。

spring:
   main:
       banner-mode: "off"

2 自定义 SpringApplication

如果SpringApplication默认值不符合您的口味,您可以改为创建本地实例并对其进行自定义。例如,要关闭您要写的横幅:

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

传递给SpringApplication的构造函数 arguments 是 spring beans 的 configuration 源。在大多数情况下,这些将是@Configuration classes 的 references,但它们也可以是 XML configuration 或应扫描的包的 reference。

也可以使用application.properties文件配置SpringApplication

二 测试

  • 自定义Banner

application.properties

spring.banner.location=banner.txt

banner.txt

                    !
/^\
/ \
| | ( ) | |
/^\ | /^\ \ / /^\ | /^\
|O| /^\ ( )|-----|( ) /^\ |O|
|_| |-| |^-^|---||-----||---|^-^| |-| |_|
|O| |O| |/^\|/^\|| | ||/^\|/^\| |O| |O|
|-| |-| ||_|||_||| /^\ |||_|||_|| |-| |-|
|O| |O| |/^\|/^\||( )||/^\|/^\| |O| |O|
|-| |-| ||_|||_|||| ||||_|||_|| |-| |-|

启动

                 !
/^\
/ \
| | ( ) | |
/^\ | /^\ \ / /^\ | /^\
|O| /^\ ( )|-----|( ) /^\ |O|
|_| |-| |^-^|---||-----||---|^-^| |-| |_|
|O| |O| |/^\|/^\|| | ||/^\|/^\| |O| |O|
|-| |-| ||_|||_||| /^\ |||_|||_|| |-| |-|
|O| |O| |/^\|/^\||( )||/^\|/^\| |O| |O|
|-| |-| ||_|||_|||| ||||_|||_|| |-| |-|
2019-10-28 22:19:03.614 INFO 18192 --- [ main] com.example.demo.TestApplication : Starting TestApplication on WGR-PC with PID 18192 (E:\STSWorkspace\test\target\classes started by asus in E:\STSWorkspace\test)
2019-10-28 22:19:03.614 INFO 18192 --- [ main] com.example.demo.TestApplication : No active profile set, falling back to default profiles: default
2019-10-28 22:19:04.394 INFO 18192 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2019-10-28 22:19:04.432 INFO 18192 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
  • 关闭Banner

spring.main.banner-mode=off
  • 带枚举

    更换Banner相对于隐藏要简单一些,我们只需要在src/main/resource下添加一个名叫banner.txt的文件,将需要修改的内容写入到该文件内就可以了,具体Banner内容如下所示:

    ${AnsiColor.BRIGHT_RED}                      !                      天地山青   ${AnsiColor.BRIGHT_YELLOW}                      !
    ${AnsiColor.BRIGHT_RED} /^\ ${AnsiColor.BRIGHT_YELLOW}道法无常 /^\
    ${AnsiColor.BRIGHT_RED} / \ 天地无极 ${AnsiColor.BRIGHT_YELLOW} / \
    ${AnsiColor.BRIGHT_RED} | | ( ) | | ${AnsiColor.BRIGHT_YELLOW}乾坤戒法 | | ( ) | |
    ${AnsiColor.BRIGHT_RED} /^\ | /^\ \ / /^\ | /^\ 元阳入体 ${AnsiColor.BRIGHT_YELLOW} /^\ | /^\ \ / /^\ | /^\
    ${AnsiColor.BRIGHT_RED} |O| /^\ ( )|-----|( ) /^\ |O| ${AnsiColor.BRIGHT_YELLOW}五毒不侵 |O| /^\ ( )|-----|( ) /^\ |O|
    ${AnsiColor.BRIGHT_RED} |_| |-| |^-^|---||-----||---|^-^| |-| |_| 九阳之体 ${AnsiColor.BRIGHT_YELLOW} |_| |-| |^-^|---||-----||---|^-^| |-| |_|
    ${AnsiColor.BRIGHT_RED} |O| |O| |/^\|/^\|| | ||/^\|/^\| |O| |O| ${AnsiColor.BRIGHT_YELLOW}化缘神功 |O| |O| |/^\|/^\|| | ||/^\|/^\| |O| |O|
    ${AnsiColor.BRIGHT_RED} |-| |-| ||_|||_||| /^\ |||_|||_|| |-| |-| 邪魔退散 ${AnsiColor.BRIGHT_YELLOW} |-| |-| ||_|||_||| /^\ |||_|||_|| |-| |-|
    ${AnsiColor.BRIGHT_RED} |O| |O| |/^\|/^\||( )||/^\|/^\| |O| |O| ${AnsiColor.BRIGHT_YELLOW}永不宕机 |O| |O| |/^\|/^\||( )||/^\|/^\| |O| |O|
    ${AnsiColor.BRIGHT_RED} |-| |-| ||_|||_|||| ||||_|||_|| |-| |-| 永无八哥 ${AnsiColor.BRIGHT_YELLOW} |-| |-| ||_|||_|||| ||||_|||_|| |-| |-|
    ${AnsiColor.BRIGHT_CYAN}

    在上面有一些属性配置,如${AnsiColor.BRIGHT_RED},这些配置都位于`org.springframework.boot.ansi.AnsiColor枚举内,用于配置的是输出的颜色。可配置内容如下所示:

      
      DEFAULT("39"),
    BLACK("30"),
    RED("31"),
    GREEN("32"),
    YELLOW("33"),
    BLUE("34"),
    MAGENTA("35"),
    CYAN("36"),
    WHITE("37"),
    BRIGHT_BLACK("90"),
    BRIGHT_RED("91"),
    BRIGHT_GREEN("92"),
    BRIGHT_YELLOW("93"),
    BRIGHT_BLUE("94"),
    BRIGHT_MAGENTA("95"),
    BRIGHT_CYAN("96"),
    BRIGHT_WHITE("97");

    这个配置是针对文字的颜色,当然还有背景颜色的配置,位于org.springframework.boot.ansi.AnsiBackground枚举内,可配置的内容如下所示:

      
     DEFAULT("49"),
    BLACK("40"),
    RED("41"),
    GREEN("42"),
    YELLOW("43"),
    BLUE("44"),
    MAGENTA("45"),
    CYAN("46"),
    WHITE("47"),
    BRIGHT_BLACK("100"),
    BRIGHT_RED("101"),
    BRIGHT_GREEN("102"),
    BRIGHT_YELLOW("103"),
    BRIGHT_BLUE("104"),
    BRIGHT_MAGENTA("105"),
    BRIGHT_CYAN("106"),
    BRIGHT_WHITE("107");

    具体的banner.txt的内容可根据自己的爱好进行配置

                          !                      天地山青                         !
    /^\ 道法无常 /^\
    / \ 天地无极 / \
    | | ( ) | | 乾坤戒法 | | ( ) | |
    /^\ | /^\ \ / /^\ | /^\ 元阳入体 /^\ | /^\ \ / /^\ | /^\
    |O| /^\ ( )|-----|( ) /^\ |O| 五毒不侵 |O| /^\ ( )|-----|( ) /^\ |O|
    |_| |-| |^-^|---||-----||---|^-^| |-| |_| 九阳之体 |_| |-| |^-^|---||-----||---|^-^| |-| |_|
    |O| |O| |/^\|/^\|| | ||/^\|/^\| |O| |O| 化缘神功 |O| |O| |/^\|/^\|| | ||/^\|/^\| |O| |O|
    |-| |-| ||_|||_||| /^\ |||_|||_|| |-| |-| 邪魔退散 |-| |-| ||_|||_||| /^\ |||_|||_|| |-| |-|
    |O| |O| |/^\|/^\||( )||/^\|/^\| |O| |O| 永不宕机 |O| |O| |/^\|/^\||( )||/^\|/^\| |O| |O|
    |-| |-| ||_|||_|||| ||||_|||_|| |-| |-| 永无八哥 |-| |-| ||_|||_|||| ||||_|||_|| |-| |-|

    2019-10-28 22:26:50.213 INFO 18256 --- [ main] com.example.demo.TestApplication : Starting TestApplication on WGR-PC with PID 18256 (E:\STSWorkspace\test\target\classes started by asus in E:\STSWorkspace\test)
    2019-10-28 22:26:50.215 INFO 18256 --- [ main] com.example.demo.TestApplication : No active profile set, falling back to default profiles: default
    2019-10-28 22:26:51.140 INFO 18256 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
    2019-10-28 22:26:51.171 INFO 18256 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]

最新文章

  1. android 自定义动画
  2. 搭建Hadoop2.5.2+Eclipse开发调试环境
  3. iOS学习系列-Apache服务器的配置
  4. LoadRunner常用事务判断
  5. Spring源码追踪2——xml解析入口
  6. BurpSuite之SQL Injection
  7. Mac安装Mysql无法登录
  8. Java开发者易犯错误Top10
  9. UOJ #278. 【UTR #2】题目排列顺序(排序水题)
  10. sns社交系统ThinkSNS+ 更新至V0.8.2,新增圈子功能
  11. 常用的Linux发行版
  12. Sql Server 里的向上取整、向下取整、四舍五入取整的实例!
  13. LightOJ1370 Bi-shoe and Phi-shoe
  14. Selector-背景选择器
  15. Linux部署Java环境
  16. C# 操作redis
  17. js 格式化数字(每三位加逗号)
  18. JZOJ 5602.【NOI2018模拟3.26】Cti
  19. C#获取一个数组中的最大值、最小值、平均值
  20. CH6202 黑暗城堡

热门文章

  1. jQuery常用方法(五)-jQuery CSS
  2. 使用 BeanDefinition 描述 Spring Bean
  3. git clone remote: HTTP Basic: Access denied
  4. python爬虫——简易天气爬取
  5. JQuery 源码解析 · extend()详解
  6. kotlin -- 可见性修饰符
  7. asp.net core 3.0 更新简记
  8. Kafka0.11之RoundRobinPartitioner/HashPartitioner(Scala):
  9. GUI tkinter (bind)事件篇
  10. 云上的芯脏病:奇怪的阿里云 RDS 数据库突发 CPU 近 100% 问题