Spring Boot Sample 002之spring-boot-banner

一、环境

  • Idea 2020.1
  • JDK 1.8
  • maven

二、目的

学习Spring Boot Banner自定义的操作。

三、步骤

3.1、点击File -> New Project -> Spring Initializer,点击next

 3.2、在对应地方修改自己的项目信息

3.3、选择Web依赖,选中Spring Web。可以选择Spring Boot版本,本次默认为2.2.6,点击Next

3.4、编辑工程名和项目路径,确定后点击Finish完成

3.5、项目结构

四、自定义Spring Boot Banner

4.1、方式一:默认配置

在resources资源包下新建banner.txt,输入自定义内容。
.__           .__  .__                               .__       .___
|  |__   ____ |  | |  |   ____   __  _  _____________|  |    __| _/
|  |  _/ __ |  | |  |  /  _    / / /  _ _  __   |   / __ |
|   Y    ___/|  |_|  |_(  <_> )       (  <_> )  | /  |__/ /_/ |
|___|  /___  >____/____/____/    /_/ ____/|__|  |____/____ |
     /     /                                                  /

spring-boot.version :  ${spring-boot.version}
spring-boot.formatted-version :${spring-boot.formatted-version}
启动程序后查看console输出
"C:Program FilesJavajdk1.8.0_221binjava.exe" -XX:TieredStopAtLevel=1 -noverify -Dspring.output.ansi.enabled=always -Dcom.sun.management.jmxremote -Dspring.jmx.enabled=true -Dspring.liveBeansView.mbeanDomain -Dspring.application.admin.enabled=true "-javaagent:D:ProgramsJetBrainsIntelliJ IDEAlibidea_rt.jar=3016:D:ProgramsJetBrainsIntelliJ IDEAbin" -Dfile.encoding=UTF-8 -classpath "C:Program FilesJavajdk1.8.0_221jrelibcharsets.jar;C:Program FilesJavajdk1.8.0_221jrelibdeploy.jar;C:Program FilesJavajdk1.8.0_221jrelibextaccess-bridge-64.jar;C:Program FilesJavajdk1.8.0_221jrelibextcldrdata.jar;C:Program FilesJavajdk1.8.0_221jrelibextdnsns.jar;C:Program FilesJavajdk1.8.0_221jrelibextjaccess.jar;C:Program FilesJavajdk1.8.0_221jrelibextjfxrt.jar;C:Program FilesJavajdk1.8.0_221jrelibextlocaledata.jar;C:Program FilesJavajdk1.8.0_221jrelibextnashorn.jar;C:Program FilesJavajdk1.8.0_221jrelibextsunec.jar;C:Program FilesJavajdk1.8.0_221jrelibextsunjce_provider.jar;C:Program FilesJavajdk1.8.0_221jrelibextsunmscapi.jar;C:Program FilesJavajdk1.8.0_221jrelibextsunpkcs11.jar;C:Program FilesJavajdk1.8.0_221jrelibextzipfs.jar;C:Program FilesJavajdk1.8.0_221jrelibjavaws.jar;C:Program FilesJavajdk1.8.0_221jrelibjce.jar;C:Program FilesJavajdk1.8.0_221jrelibjfr.jar;C:Program FilesJavajdk1.8.0_221jrelibjfxswt.jar;C:Program FilesJavajdk1.8.0_221jrelibjsse.jar;C:Program FilesJavajdk1.8.0_221jrelibmanagement-agent.jar;C:Program FilesJavajdk1.8.0_221jrelibplugin.jar;C:Program FilesJavajdk1.8.0_221jrelibresources.jar;C:Program FilesJavajdk1.8.0_221jrelibrt.jar;D:WorkSpaceIdeaouyushanouyushan-spring-boot-samplesspring-boot-bannertargetclasses;D:Programsapachemavenapache-maven-repositoryorgspringframeworkbootspring-boot-starter-web2.2.6.RELEASEspring-boot-starter-web-2.2.6.RELEASE.jar;D:Programsapachemavenapache-maven-repositoryorgspringframeworkbootspring-boot-starter2.2.6.RELEASEspring-boot-starter-2.2.6.RELEASE.jar;D:Programsapachemavenapache-maven-repositoryorgspringframeworkbootspring-boot2.2.6.RELEASEspring-boot-2.2.6.RELEASE.jar;D:Programsapachemavenapache-maven-repositoryorgspringframeworkbootspring-boot-autoconfigure2.2.6.RELEASEspring-boot-autoconfigure-2.2.6.RELEASE.jar;D:Programsapachemavenapache-maven-repositoryorgspringframeworkbootspring-boot-starter-logging2.2.6.RELEASEspring-boot-starter-logging-2.2.6.RELEASE.jar;D:Programsapachemavenapache-maven-repositorychqoslogbacklogback-classic1.2.3logback-classic-1.2.3.jar;D:Programsapachemavenapache-maven-repositorychqoslogbacklogback-core1.2.3logback-core-1.2.3.jar;D:Programsapachemavenapache-maven-repositoryorgapachelogginglog4jlog4j-to-slf4j2.12.1log4j-to-slf4j-2.12.1.jar;D:Programsapachemavenapache-maven-repositoryorgapachelogginglog4jlog4j-api2.12.1log4j-api-2.12.1.jar;D:Programsapachemavenapache-maven-repositoryorgslf4jjul-to-slf4j1.7.30jul-to-slf4j-1.7.30.jar;D:Programsapachemavenapache-maven-repositoryjakartaannotationjakarta.annotation-api1.3.5jakarta.annotation-api-1.3.5.jar;D:Programsapachemavenapache-maven-repositoryorgyamlsnakeyaml1.25snakeyaml-1.25.jar;D:Programsapachemavenapache-maven-repositoryorgspringframeworkbootspring-boot-starter-json2.2.6.RELEASEspring-boot-starter-json-2.2.6.RELEASE.jar;D:Programsapachemavenapache-maven-repositorycomfasterxmljacksoncorejackson-databind2.10.3jackson-databind-2.10.3.jar;D:Programsapachemavenapache-maven-repositorycomfasterxmljacksoncorejackson-annotations2.10.3jackson-annotations-2.10.3.jar;D:Programsapachemavenapache-maven-repositorycomfasterxmljacksoncorejackson-core2.10.3jackson-core-2.10.3.jar;D:Programsapachemavenapache-maven-repositorycomfasterxmljacksondatatypejackson-datatype-jdk82.10.3jackson-datatype-jdk8-2.10.3.jar;D:Programsapachemavenapache-maven-repositorycomfasterxmljacksondatatypejackson-datatype-jsr3102.10.3jackson-datatype-jsr310-2.10.3.jar;D:Programsapachemavenapache-maven-repositorycomfasterxmljacksonmodulejackson-module-parameter-names2.10.3jackson-module-parameter-names-2.10.3.jar;D:Programsapachemavenapache-maven-repositoryorgspringframeworkbootspring-boot-starter-tomcat2.2.6.RELEASEspring-boot-starter-tomcat-2.2.6.RELEASE.jar;D:Programsapachemavenapache-maven-repositoryorgapachetomcatembedtomcat-embed-core9.0.33tomcat-embed-core-9.0.33.jar;D:Programsapachemavenapache-maven-repositoryorgapachetomcatembedtomcat-embed-el9.0.33tomcat-embed-el-9.0.33.jar;D:Programsapachemavenapache-maven-repositoryorgapachetomcatembedtomcat-embed-websocket9.0.33tomcat-embed-websocket-9.0.33.jar;D:Programsapachemavenapache-maven-repositoryorgspringframeworkbootspring-boot-starter-validation2.2.6.RELEASEspring-boot-starter-validation-2.2.6.RELEASE.jar;D:Programsapachemavenapache-maven-repositoryjakartavalidationjakarta.validation-api2.0.2jakarta.validation-api-2.0.2.jar;D:Programsapachemavenapache-maven-repositoryorghibernatevalidatorhibernate-validator6.0.18.Finalhibernate-validator-6.0.18.Final.jar;D:Programsapachemavenapache-maven-repositoryorgjbossloggingjboss-logging3.4.1.Finaljboss-logging-3.4.1.Final.jar;D:Programsapachemavenapache-maven-repositorycomfasterxmlclassmate1.5.1classmate-1.5.1.jar;D:Programsapachemavenapache-maven-repositoryorgspringframeworkspring-web5.2.5.RELEASEspring-web-5.2.5.RELEASE.jar;D:Programsapachemavenapache-maven-repositoryorgspringframeworkspring-beans5.2.5.RELEASEspring-beans-5.2.5.RELEASE.jar;D:Programsapachemavenapache-maven-repositoryorgspringframeworkspring-webmvc5.2.5.RELEASEspring-webmvc-5.2.5.RELEASE.jar;D:Programsapachemavenapache-maven-repositoryorgspringframeworkspring-aop5.2.5.RELEASEspring-aop-5.2.5.RELEASE.jar;D:Programsapachemavenapache-maven-repositoryorgspringframeworkspring-context5.2.5.RELEASEspring-context-5.2.5.RELEASE.jar;D:Programsapachemavenapache-maven-repositoryorgspringframeworkspring-expression5.2.5.RELEASEspring-expression-5.2.5.RELEASE.jar;D:Programsapachemavenapache-maven-repositoryorgslf4jslf4j-api1.7.30slf4j-api-1.7.30.jar;D:Programsapachemavenapache-maven-repositoryorgspringframeworkspring-core5.2.5.RELEASEspring-core-5.2.5.RELEASE.jar;D:Programsapachemavenapache-maven-repositoryorgspringframeworkspring-jcl5.2.5.RELEASEspring-jcl-5.2.5.RELEASE.jar" org.ouyushan.springboot.banner.SpringBootBannerApplication
.__           .__  .__                               .__       .___
|  |__   ____ |  | |  |   ____   __  _  _____________|  |    __| _/
|  |  _/ __ |  | |  |  /  _    / / /  _ _  __   |   / __ |
|   Y    ___/|  |_|  |_(  <_> )       (  <_> )  | /  |__/ /_/ |
|___|  /___  >____/____/____/    /_/ ____/|__|  |____/____ |
     /     /                                                  /

spring-boot.version :  2.2.6.RELEASE
spring-boot.formatted-version : (v2.2.6.RELEASE)

2020-04-27 10:37:31.150  INFO 8696 --- [           main] o.o.s.b.SpringBootBannerApplication      : Starting SpringBootBannerApplication

 

4.2、方式二:自定义文本banner配置

在application.properties配置文件中指定
## 指定文本文件位置 spring.banner.location=/txt/banner.txt 

在resourcesz资源包中新建/txt/banner.txt文件,配置内容
.__           .__  .__                               .__       .___
|  |__   ____ |  | |  |   ____   __  _  _____________|  |    __| _/
|  |  _/ __ |  | |  |  /  _    / / /  _ _  __   |   / __ |
|   Y    ___/|  |_|  |_(  <_> )       (  <_> )  | /  |__/ /_/ |
|___|  /___  >____/____/____/    /_/ ____/|__|  |____/____ |
     /     /                                                  /

指定banner位置:resource/txt
spring-boot.version :  ${spring-boot.version}
spring-boot.formatted-version :${spring-boot.formatted-version}

 

启动项目后,console输出如下,此时自定义/txt/banner.txt会覆盖banner.txt
"C:Program FilesJavajdk1.8.0_221binjava.exe" -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:3230,suspend=y,server=n -XX:TieredStopAtLevel=1 -noverify -Dspring.output.ansi.enabled=always -Dcom.sun.management.jmxremote -Dspring.jmx.enabled=true -Dspring.liveBeansView.mbeanDomain -Dspring.application.admin.enabled=true -javaagent:C:UsersyangyanglleiAppDataLocalJetBrainsIntelliJIdea2020.1captureAgentdebugger-agent.jar -Dfile.encoding=UTF-8 -classpath "C:Program FilesJavajdk1.8.0_221jrelibcharsets.jar;C:Program FilesJavajdk1.8.0_221jrelibdeploy.jar;C:Program FilesJavajdk1.8.0_221jrelibextaccess-bridge-64.jar;C:Program FilesJavajdk1.8.0_221jrelibextcldrdata.jar;C:Program FilesJavajdk1.8.0_221jrelibextdnsns.jar;C:Program FilesJavajdk1.8.0_221jrelibextjaccess.jar;C:Program FilesJavajdk1.8.0_221jrelibextjfxrt.jar;C:Program FilesJavajdk1.8.0_221jrelibextlocaledata.jar;C:Program FilesJavajdk1.8.0_221jrelibextnashorn.jar;C:Program FilesJavajdk1.8.0_221jrelibextsunec.jar;C:Program FilesJavajdk1.8.0_221jrelibextsunjce_provider.jar;C:Program FilesJavajdk1.8.0_221jrelibextsunmscapi.jar;C:Program FilesJavajdk1.8.0_221jrelibextsunpkcs11.jar;C:Program FilesJavajdk1.8.0_221jrelibextzipfs.jar;C:Program FilesJavajdk1.8.0_221jrelibjavaws.jar;C:Program FilesJavajdk1.8.0_221jrelibjce.jar;C:Program FilesJavajdk1.8.0_221jrelibjfr.jar;C:Program FilesJavajdk1.8.0_221jrelibjfxswt.jar;C:Program FilesJavajdk1.8.0_221jrelibjsse.jar;C:Program FilesJavajdk1.8.0_221jrelibmanagement-agent.jar;C:Program FilesJavajdk1.8.0_221jrelibplugin.jar;C:Program FilesJavajdk1.8.0_221jrelibresources.jar;C:Program FilesJavajdk1.8.0_221jrelibrt.jar;D:WorkSpaceIdeaouyushanouyushan-spring-boot-samplesspring-boot-bannertargetclasses;D:Programsapachemavenapache-maven-repositoryorgspringframeworkbootspring-boot-starter-web2.2.6.RELEASEspring-boot-starter-web-2.2.6.RELEASE.jar;D:Programsapachemavenapache-maven-repositoryorgspringframeworkbootspring-boot-starter2.2.6.RELEASEspring-boot-starter-2.2.6.RELEASE.jar;D:Programsapachemavenapache-maven-repositoryorgspringframeworkbootspring-boot2.2.6.RELEASEspring-boot-2.2.6.RELEASE.jar;D:Programsapachemavenapache-maven-repositoryorgspringframeworkbootspring-boot-autoconfigure2.2.6.RELEASEspring-boot-autoconfigure-2.2.6.RELEASE.jar;D:Programsapachemavenapache-maven-repositoryorgspringframeworkbootspring-boot-starter-logging2.2.6.RELEASEspring-boot-starter-logging-2.2.6.RELEASE.jar;D:Programsapachemavenapache-maven-repositorychqoslogbacklogback-classic1.2.3logback-classic-1.2.3.jar;D:Programsapachemavenapache-maven-repositorychqoslogbacklogback-core1.2.3logback-core-1.2.3.jar;D:Programsapachemavenapache-maven-repositoryorgapachelogginglog4jlog4j-to-slf4j2.12.1log4j-to-slf4j-2.12.1.jar;D:Programsapachemavenapache-maven-repositoryorgapachelogginglog4jlog4j-api2.12.1log4j-api-2.12.1.jar;D:Programsapachemavenapache-maven-repositoryorgslf4jjul-to-slf4j1.7.30jul-to-slf4j-1.7.30.jar;D:Programsapachemavenapache-maven-repositoryjakartaannotationjakarta.annotation-api1.3.5jakarta.annotation-api-1.3.5.jar;D:Programsapachemavenapache-maven-repositoryorgyamlsnakeyaml1.25snakeyaml-1.25.jar;D:Programsapachemavenapache-maven-repositoryorgspringframeworkbootspring-boot-starter-json2.2.6.RELEASEspring-boot-starter-json-2.2.6.RELEASE.jar;D:Programsapachemavenapache-maven-repositorycomfasterxmljacksoncorejackson-databind2.10.3jackson-databind-2.10.3.jar;D:Programsapachemavenapache-maven-repositorycomfasterxmljacksoncorejackson-annotations2.10.3jackson-annotations-2.10.3.jar;D:Programsapachemavenapache-maven-repositorycomfasterxmljacksoncorejackson-core2.10.3jackson-core-2.10.3.jar;D:Programsapachemavenapache-maven-repositorycomfasterxmljacksondatatypejackson-datatype-jdk82.10.3jackson-datatype-jdk8-2.10.3.jar;D:Programsapachemavenapache-maven-repositorycomfasterxmljacksondatatypejackson-datatype-jsr3102.10.3jackson-datatype-jsr310-2.10.3.jar;D:Programsapachemavenapache-maven-repositorycomfasterxmljacksonmodulejackson-module-parameter-names2.10.3jackson-module-parameter-names-2.10.3.jar;D:Programsapachemavenapache-maven-repositoryorgspringframeworkbootspring-boot-starter-tomcat2.2.6.RELEASEspring-boot-starter-tomcat-2.2.6.RELEASE.jar;D:Programsapachemavenapache-maven-repositoryorgapachetomcatembedtomcat-embed-core9.0.33tomcat-embed-core-9.0.33.jar;D:Programsapachemavenapache-maven-repositoryorgapachetomcatembedtomcat-embed-el9.0.33tomcat-embed-el-9.0.33.jar;D:Programsapachemavenapache-maven-repositoryorgapachetomcatembedtomcat-embed-websocket9.0.33tomcat-embed-websocket-9.0.33.jar;D:Programsapachemavenapache-maven-repositoryorgspringframeworkbootspring-boot-starter-validation2.2.6.RELEASEspring-boot-starter-validation-2.2.6.RELEASE.jar;D:Programsapachemavenapache-maven-repositoryjakartavalidationjakarta.validation-api2.0.2jakarta.validation-api-2.0.2.jar;D:Programsapachemavenapache-maven-repositoryorghibernatevalidatorhibernate-validator6.0.18.Finalhibernate-validator-6.0.18.Final.jar;D:Programsapachemavenapache-maven-repositoryorgjbossloggingjboss-logging3.4.1.Finaljboss-logging-3.4.1.Final.jar;D:Programsapachemavenapache-maven-repositorycomfasterxmlclassmate1.5.1classmate-1.5.1.jar;D:Programsapachemavenapache-maven-repositoryorgspringframeworkspring-web5.2.5.RELEASEspring-web-5.2.5.RELEASE.jar;D:Programsapachemavenapache-maven-repositoryorgspringframeworkspring-beans5.2.5.RELEASEspring-beans-5.2.5.RELEASE.jar;D:Programsapachemavenapache-maven-repositoryorgspringframeworkspring-webmvc5.2.5.RELEASEspring-webmvc-5.2.5.RELEASE.jar;D:Programsapachemavenapache-maven-repositoryorgspringframeworkspring-aop5.2.5.RELEASEspring-aop-5.2.5.RELEASE.jar;D:Programsapachemavenapache-maven-repositoryorgspringframeworkspring-context5.2.5.RELEASEspring-context-5.2.5.RELEASE.jar;D:Programsapachemavenapache-maven-repositoryorgspringframeworkspring-expression5.2.5.RELEASEspring-expression-5.2.5.RELEASE.jar;D:Programsapachemavenapache-maven-repositoryorgslf4jslf4j-api1.7.30slf4j-api-1.7.30.jar;D:Programsapachemavenapache-maven-repositoryorgspringframeworkspring-core5.2.5.RELEASEspring-core-5.2.5.RELEASE.jar;D:Programsapachemavenapache-maven-repositoryorgspringframeworkspring-jcl5.2.5.RELEASEspring-jcl-5.2.5.RELEASE.jar;D:ProgramsJetBrainsIntelliJ IDEAlibidea_rt.jar" org.ouyushan.springboot.banner.SpringBootBannerApplication
Connected to the target VM, address: '127.0.0.1:3230', transport: 'socket'
.__           .__  .__                               .__       .___
|  |__   ____ |  | |  |   ____   __  _  _____________|  |    __| _/
|  |  _/ __ |  | |  |  /  _    / / /  _ _  __   |   / __ |
|   Y    ___/|  |_|  |_(  <_> )       (  <_> )  | /  |__/ /_/ |
|___|  /___  >____/____/____/    /_/ ____/|__|  |____/____ |
     /     /                                                  /

指定banner位置:resource/txt
spring-boot.version :  2.2.6.RELEASE
spring-boot.formatted-version : (v2.2.6.RELEASE)

4.3、方式三:自定义图片banner配置

 在application.properties配置文件中指定

## 指定文本文件位置
# spring.banner.location=/txt/banner.txt

## 指定图片文件位置
spring.banner.image.location=/image/banner.gif
 
在resourcese资源包中新建/image,将下图命名为banner.gif后存入image目录
启动项目后,console会逐贞输出gif图片

 

五、知识点

## 1、项目名称:
```
spring-boot-banner
```
## 2、相关概念:
* spring boot banner 可以是文本也可以是动画,同时存在时动画优先于文本显示。
* spring boot 启动时会默认从classpath中加载banner.txt,若存在则打印里面的内容。
* banner.txt中可包含以下等变量:
```
${application.version}
${application.formatted-version}
${spring-boot.version}
${spring-boot.formatted-version}
${application.title}
```

* 通过以下配置来指定banner文本文件的位置,默认在根目录下,如resources

```
spring.banner.location=/txt/banner.txt
```

* 通过以下配置来指定banner文本文件的编码,默认UTF-8
```
spring.banner.charset
```

* 支持的图片文件为banner.gif、banner.jpg和banner.png。图片文件会被转换为ASCII art,并优先于文本banner展示。
* 通过以下配置来指定banner图像文件的位置,默认在根目录下,如resources
```
spring.banner.image.location=/image/banner.gif
```

* 通过以下配置来指定banner状态和级别,不配置时默认开启console
```
# spring.main.banner-mode=console
# spring.main.banner-mode=log
## 关闭banner
# spring.main.banner-mode=off
```

## 3、sample实现
* 文本banner
在resources下新建文件banner.txt
```
.__           .__  .__                               .__       .___
|  |__   ____ |  | |  |   ____   __  _  _____________|  |    __| _/
|  |  _/ __ |  | |  |  /  _    / / /  _ _  __   |   / __ |
|   Y    ___/|  |_|  |_(  <_> )       (  <_> )  | /  |__/ /_/ |
|___|  /___  >____/____/____/    /_/ ____/|__|  |____/____ |
     /     /                                                  /

spring-boot.version : ${spring-boot.version}
spring-boot.formatted-version :${spring-boot.formatted-version}
```
打印信息
```

.__           .__  .__                               .__       .___
|  |__   ____ |  | |  |   ____   __  _  _____________|  |    __| _/
|  |  _/ __ |  | |  |  /  _    / / /  _ _  __   |   / __ |
|   Y    ___/|  |_|  |_(  <_> )       (  <_> )  | /  |__/ /_/ |
|___|  /___  >____/____/____/    /_/ ____/|__|  |____/____ |
     /     /                                                  /

spring-boot.version : 2.2.6.RELEASE
spring-boot.formatted-version : (v2.2.6.RELEASE)
```

* 图片banner
在resources下保存spring官方banner.gif

 

posted @ 2020-06-01 10:08  欧虞山  阅读(236)  评论(0编辑  收藏  举报