Like Share Discussion Bookmark Smile

J.J. Huang   2019-04-29   Spring Boot   瀏覽次數:次   DMCA.com Protection Status

SpringBoot - 第四十章 | banner 更改

最近有點累,畢竟堅持著寫部落格,增進自己技術能力是漫長的,今天讓自己稍微放鬆一點,找了一個比較有趣的單元來實作。
這一篇介紹如何自定義springboot的啟動banner。


原始 Banner

有在使用SpringBoot的人都知道,當啟動應用後原生Banner就如下方。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

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

2019-04-29 11:29:15.230 INFO 1304 --- [ main] c.j.l.s.chapter40.Chapter40Application : Starting Chapter40Application on localhost with PID 1304 (/Users/morose/Documents/workspace-SpringBoot/chapter-40/target/classes started by morose in /Users/morose/Documents/workspace-SpringBoot/chapter-40)
2019-04-29 11:29:15.232 INFO 1304 --- [ main] c.j.l.s.chapter40.Chapter40Application : No active profile set, falling back to default profiles: default
2019-04-29 11:29:16.302 INFO 1304 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2019-04-29 11:29:16.337 INFO 1304 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-04-29 11:29:16.337 INFO 1304 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.17]
2019-04-29 11:29:16.449 INFO 1304 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-04-29 11:29:16.449 INFO 1304 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1151 ms
2019-04-29 11:29:16.698 INFO 1304 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-04-29 11:29:16.933 INFO 1304 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2019-04-29 11:29:16.936 INFO 1304 --- [ main] c.j.l.s.chapter40.Chapter40Application : Started Chapter40Application in 2.041 seconds (JVM running for 2.43)
s

自定義Banner


測試結果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
    _____        _____     __    __                                         
/ | / | / | / |
$$$$$ | $$$$$ | $$ | $$ | __ __ ______ _______ ______
$$ | $$ | $$ |__$$ |/ | / | / \ / \ / \
__ $$ | __ $$ | $$ $$ |$$ | $$ | $$$$$$ |$$$$$$$ |/$$$$$$ |
/ | $$ | / | $$ | $$$$$$$$ |$$ | $$ | / $$ |$$ | $$ |$$ | $$ |
$$ \__$$ |__ $$ \__$$ |__ $$ | $$ |$$ \__$$ |/$$$$$$$ |$$ | $$ |$$ \__$$ |
$$ $$// |$$ $$// |$$ | $$ |$$ $$/ $$ $$ |$$ | $$ |$$ $$ |
$$$$$$/ $$/ $$$$$$/ $$/ $$/ $$/ $$$$$$/ $$$$$$$/ $$/ $$/ $$$$$$$ |
/ \__$$ |
$$ $$/
$$$$$$/
2019-04-29 11:34:47.117 INFO 1314 --- [ main] c.j.l.s.chapter40.Chapter40Application : Starting Chapter40Application on localhost with PID 1314 (/Users/morose/Documents/workspace-SpringBoot/chapter-40/target/classes started by morose in /Users/morose/Documents/workspace-SpringBoot/chapter-40)
2019-04-29 11:34:47.120 INFO 1314 --- [ main] c.j.l.s.chapter40.Chapter40Application : No active profile set, falling back to default profiles: default
2019-04-29 11:34:48.149 INFO 1314 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2019-04-29 11:34:48.181 INFO 1314 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-04-29 11:34:48.181 INFO 1314 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.17]
2019-04-29 11:34:48.299 INFO 1314 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-04-29 11:34:48.299 INFO 1314 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1120 ms
2019-04-29 11:34:48.591 INFO 1314 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-04-29 11:34:48.829 INFO 1314 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2019-04-29 11:34:48.832 INFO 1314 --- [ main] c.j.l.s.chapter40.Chapter40Application : Started Chapter40Application in 2.026 seconds (JVM running for 2.347)

屬性設置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
${AnsiColor.BRIGHT_GREEN}

_____ _____ __ __
/ | / | / | / |
$$$$$ | $$$$$ | $$ | $$ | __ __ ______ _______ ______
$$ | $$ | $$ |__$$ |/ | / | / \ / \ / \
__ $$ | __ $$ | $$ $$ |$$ | $$ | $$$$$$ |$$$$$$$ |/$$$$$$ |
/ | $$ | / | $$ | $$$$$$$$ |$$ | $$ | / $$ |$$ | $$ |$$ | $$ |
$$ \__$$ |__ $$ \__$$ |__ $$ | $$ |$$ \__$$ |/$$$$$$$ |$$ | $$ |$$ \__$$ |
$$ $$// |$$ $$// |$$ | $$ |$$ $$/ $$ $$ |$$ | $$ |$$ $$ |
$$$$$$/ $$/ $$$$$$/ $$/ $$/ $$/ $$$$$$/ $$$$$$$/ $$/ $$/ $$$$$$$ |
/ \__$$ |
$$ $$/
$$$$$$/

${AnsiColor.BRIGHT_RED}
Application Version: ${application.version}${application.formatted-version}
Spring Boot Version: ${spring-boot.version}${spring-boot.formatted-version}
  • ${AnsiColor.BRIGHT_RED}:設置控制台中輸出內容的顏色
  • ${application.version}:用來獲取MANIFEST.MF文件中的版本號
  • ${application.formatted-version}:格式化後的${application.version}版本訊息
  • ${spring-boot.version}:Spring Boot的版本號
  • ${spring-boot.formatted-version}:格式化後的${spring-boot.version}版本訊息

測試結果

因為我使用Eclipse所以Banner顏色好像不起作用。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28


_____ _____ __ __
/ | / | / | / |
$$$$$ | $$$$$ | $$ | $$ | __ __ ______ _______ ______
$$ | $$ | $$ |__$$ |/ | / | / \ / \ / \
__ $$ | __ $$ | $$ $$ |$$ | $$ | $$$$$$ |$$$$$$$ |/$$$$$$ |
/ | $$ | / | $$ | $$$$$$$$ |$$ | $$ | / $$ |$$ | $$ |$$ | $$ |
$$ \__$$ |__ $$ \__$$ |__ $$ | $$ |$$ \__$$ |/$$$$$$$ |$$ | $$ |$$ \__$$ |
$$ $$// |$$ $$// |$$ | $$ |$$ $$/ $$ $$ |$$ | $$ |$$ $$ |
$$$$$$/ $$/ $$$$$$/ $$/ $$/ $$/ $$$$$$/ $$$$$$$/ $$/ $$/ $$$$$$$ |
/ \__$$ |
$$ $$/
$$$$$$/


Application Version:
Spring Boot Version: 2.1.4.RELEASE (v2.1.4.RELEASE)
2019-04-29 11:42:58.073 INFO 1331 --- [ main] c.j.l.s.chapter40.Chapter40Application : Starting Chapter40Application on localhost with PID 1331 (/Users/morose/Documents/workspace-SpringBoot/chapter-40/target/classes started by morose in /Users/morose/Documents/workspace-SpringBoot/chapter-40)
2019-04-29 11:42:58.076 INFO 1331 --- [ main] c.j.l.s.chapter40.Chapter40Application : No active profile set, falling back to default profiles: default
2019-04-29 11:42:59.127 INFO 1331 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2019-04-29 11:42:59.158 INFO 1331 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-04-29 11:42:59.158 INFO 1331 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.17]
2019-04-29 11:42:59.271 INFO 1331 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-04-29 11:42:59.272 INFO 1331 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1127 ms
2019-04-29 11:42:59.509 INFO 1331 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-04-29 11:42:59.732 INFO 1331 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2019-04-29 11:42:59.735 INFO 1331 --- [ main] c.j.l.s.chapter40.Chapter40Application : Started Chapter40Application in 1.97 seconds (JVM running for 2.307)

動態Banner

SpringBoot可以代印動態Banner。找一張自己喜歡的gif圖片如下

在resources文件夾下新建banner.gif。

控制台打印的時候會將gif的每一幀轉成ascii碼,打印在控制台。而且,打印完畢後,還會將banner.txt中的內容打印出來。


測試結果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175

















..
.::*
...


















..... ....
........ . ...
........ . ..
...... ....
.... ...
. .







..**::**..
.*::::::::::*.
.*::::::::::::*.
.*::::::::::::*.
.*::::::::::::*.
.*::::::::::*.
..**::***.







..
..... ..
..... ...
...... ......
. ... . .....
. .... . .



............................ .............
.................. ......... ................ .
................... ... . ... ............
............. . ... ...............
. ......... ...........
.......... ....... ....
............ ........
........ ........
.... . .........
........ ........
......... ..********.. ......*..
........ .**::::::::::::**. ........
........... .**::::::::::::::::**. .......
...... .*::::::::::::::::::::*. .......
... .. .*::::::::::::::::::::::*. .......
...... .::::::::::::::::::::::::. .......
.......... .::::::::::::::::::::::::. ... ....
......... .*:::::::::::::::::::::::. ....
......... .*::::::::::::::::::::::*. ...
......... .*::::::::::::::::::::*. ....
........ .**::::::::::::::::**. .........
... .... .**::::::::::::**. .........
. ........ .********.. .........
....... . ......*..
......... . .....
....... .........
........ . ............
............ .. ...........
. ............. .........
................ .... ..........
............. .... . ......... . ..... .
.... ...... ......... . .. .... .............. ....
.. ............. ........... ..............


中間省略⋯⋯





*::::::::::::::*
::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::: :.
*::::::::::::::::::::::::::::::::::::: :*:
::::::::::::::::::::::::::::::::::::::::::: *:
.:::::::::::::::::::::::::::::::::::::::::: :
::::::::::::::::::::::::::::::::::::::::::. .:
::::::::::::::::::::::::::::::::::::::::: :
:::::::::::::::::::::::::::::::::::::: *:
:::::::::::::::::::::::::::::::::* :
:::::::::::::::::::* :
::::::::::::::: * *:
.:::::::::::: * .:
:::::::::::. : :
:::::::::: : .:
:::::::::: :: .:
.:::::::: ::: :*
:::::::: ::: :
:::::::: :::: ::
::::::: ::::: *:
::::::: *::::::: ::
:::::::. ::::::::::: :::
:::::::::::::::::::::::* ::::
.::: :::::::::* .:::::*
: ::::::::::::::::::::::::::::::::::::
. .:::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::
*::::::::::::::*





_____ _____ __ __
/ | / | / | / |
$$$$$ | $$$$$ | $$ | $$ | __ __ ______ _______ ______
$$ | $$ | $$ |__$$ |/ | / | / \ / \ / \
__ $$ | __ $$ | $$ $$ |$$ | $$ | $$$$$$ |$$$$$$$ |/$$$$$$ |
/ | $$ | / | $$ | $$$$$$$$ |$$ | $$ | / $$ |$$ | $$ |$$ | $$ |
$$ \__$$ |__ $$ \__$$ |__ $$ | $$ |$$ \__$$ |/$$$$$$$ |$$ | $$ |$$ \__$$ |
$$ $$// |$$ $$// |$$ | $$ |$$ $$/ $$ $$ |$$ | $$ |$$ $$ |
$$$$$$/ $$/ $$$$$$/ $$/ $$/ $$/ $$$$$$/ $$$$$$$/ $$/ $$/ $$$$$$$ |
/ \__$$ |
$$ $$/
$$$$$$/


Application Version:
Spring Boot Version: 2.1.4.RELEASE (v2.1.4.RELEASE)
2019-04-29 11:54:59.990 INFO 1382 --- [ main] c.j.l.s.chapter40.Chapter40Application : Starting Chapter40Application on localhost with PID 1382 (/Users/morose/Documents/workspace-SpringBoot/chapter-40/target/classes started by morose in /Users/morose/Documents/workspace-SpringBoot/chapter-40)
2019-04-29 11:54:59.994 INFO 1382 --- [ main] c.j.l.s.chapter40.Chapter40Application : No active profile set, falling back to default profiles: default
2019-04-29 11:55:01.218 INFO 1382 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2019-04-29 11:55:01.257 INFO 1382 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-04-29 11:55:01.257 INFO 1382 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.17]
2019-04-29 11:55:01.423 INFO 1382 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-04-29 11:55:01.423 INFO 1382 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1373 ms
2019-04-29 11:55:01.706 INFO 1382 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-04-29 11:55:01.983 INFO 1382 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2019-04-29 11:55:01.987 INFO 1382 --- [ main] c.j.l.s.chapter40.Chapter40Application : Started Chapter40Application in 16.473 seconds (JVM running for 16.828)

註:以上參考了
SpringBoot启动banner更改
程序猿DD新年彩蛋:Spring Boot自定义Banner 文章。