Spring Boot 2.0 提供了不少新特性,其中就有一個小彩蛋:動態 Banner,今天咱們就先拿這個來嚐嚐鮮。
php
使用 Spring Boot 2.0 首先須要將項目依賴包替換爲剛剛發佈的 2.0 RELEASE,如今網站https://start.spring.io/
也將 Spring Boot 2.0 設置爲默認版本。java
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RELEASE</version> </parent>
設置完畢後,dependencies
中沒有指明版本的依賴包,將自動使用2.0.0.RELEASE依賴的版本。linux
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> </dependencies>
Spring Boot 2.0 剛剛發佈,一些 Maven 倉庫還沒更新,若是導入項目後依賴包不能下載,能夠手動添加 Spring Boot 官方 Maven 倉庫。android
<repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/libs-milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories>
你們作好心理準備,第一次使用 Spring Boot 2.0 ,完整依賴包須要下載半小時左右。git
咱們先來回顧一下在 Spring Boot 1.0 中如何更換啓動 Banner,其實都很簡單,只須要在src/main/resources
路徑下新建一個banner.txt
文件,banner.txt
中填寫好須要打印的字符串內容便可。程序員
通常狀況下,咱們會藉助第三方工具幫忙轉化內容,如網站http://www.network-science.de/ascii/
將文字轉化成字符串,網站:http://www.degraeve.com/img2txt.php
能夠將圖片轉化成字符串。github
咱們以Hello World爲啓動字符串來進行測試:redis
.__ .__ .__ .__ .___
| |__ ____ | | | | ____ __ _ _____________| | __| _/
| | \_/ __ \| | | | / _ \ \ \/ \/ / _ \_ __ \ | / __ |
| Y \ ___/| |_| |_( <_> ) \ ( <_> ) | \/ |__/ /_/ | |___| /\___ >____/____/\____/ \/\_/ \____/|__| |____/\____ | \/ \/ \/
在 Spring Boot 2.0 項目src/main/resources
路徑下新建一個banner.txt
文件,粘貼上述字符串,啓動 Spring Boot 項目觀察啓動日誌,發現 Spring Boot 2.0 已經將默認的 Spring 字符串替換爲 hello world。說明 Spring Boot 2.0 也支持1.0更換 Banner 的方法。spring
接下來在 Spring Boot 2.0 項目中測試打印動態 Banner ,使用的gif以下:mongodb
一樣咱們將 banner.gif 文件放到項目的src/main/resources
路徑下,啓動項目進行測試,輸出欄打印信息以下:
.. .::* ... ..... .... ........ . ... ........ . .. ...... .... .... ... . . ..**::**.. .*::::::::::*. .*::::::::::::*. .*::::::::::::*. .*::::::::::::*. .*::::::::::*. ..**::***. .. ..... .. ..... ... ...... ...... . ... . ..... . .... . . ............................ ............. .................. ......... ................ . ................... ... . ... ............ ............. . ... ............... . ......... ........... .......... ....... .... ............ ........ ........ ........ .... . ......... ........ ........ ......... ..********.. ......*.. ........ .**::::::::::::**. ........ ........... .**::::::::::::::::**. ....... ...... .*::::::::::::::::::::*. ....... ... .. .*::::::::::::::::::::::*. ....... ...... .::::::::::::::::::::::::. ....... .......... .::::::::::::::::::::::::. ... .... ......... .*:::::::::::::::::::::::. .... ......... .*::::::::::::::::::::::*. ... ......... .*::::::::::::::::::::*. .... ........ .**::::::::::::::::**. ......... ... .... .**::::::::::::**. ......... . ........ .********.. ......... ....... . ......*.. ......... . ..... ....... ......... ........ . ............ ............ .. ........... . ............. ......... ................ .... .......... ............. .... . ......... . ..... . .... ...... ......... . .. .... .............. .... .. ............. ........... .............. ::. .*:* :. *:* *. .*:* .:*. *: .*:*. :. .: :* : :. .::::* : : : * *****..... *.: : : : .:* .::::::::::**.. .. : * : *: .*:::::::::::::::*. * ** o *** ...**::::::::::::::::::**. * :.: : .* : .....***::::::::::::::::::::::*. : : o :*. * ..*****:::::::::::::::::::::::::*. * : o : . .*::::::::::::::::::::::::::::::::*. :: * :* : *::::::::::::::::::::::::::::::::::* : o * o * .*::::::::::::::::::::::::::::::::::*. .:o : : : : .*::::::::::::::::::::::::::::::::::*. : . : : .. .*::::::::::::::::::::::::::::::::::*. * : * : : .*::::::::::::::::::::::::::::::::::* : *: . :* .*o:::::::::::::::::::::::::::::::*. . : : : * .*::::::::::::::::::::::::::::::*. * :* o * : .*::::::::::::::::::::::::::::*. : ** : :* * .*::::::::::::::::::::::::**. *.. * ** * ..*::::::::::::::::::::*.. :* * * o .. ..**::::::::::::**.. .:. : : : o.: ...******... *. : :. : ::o:. *: * *: :. : .*:*. :* .*:: .:* .* *o: .: .:*. .*: ...
經過上述輸出咱們發現 Spring Boot 在啓動的時候,會將 gif 圖片的每個畫面,按照順序打印在日誌中,全部的畫面打印完畢後,纔會啓動 Spring Boot 項目。
若是目錄src/main/resources
下同時存在banner.txt
和banner.gif
,項目會先將banner.gif
每個畫面打印完畢以後,再打印banner.txt
中的內容。
項目的啓動 Banner 有什麼用呢,在一些大的組織或者公司中,能夠利用這個特性定製本身專屬的啓動畫面,增長團隊對品牌的認同感。
業餘草分享100套精選1000G架構師資料課程(超1T的IT學習資料免費送)。
超過1024G的IT學習資料免費領取,你值得擁有!
領取資源方式,關注「業餘草」公衆號,回覆對應的關鍵字
本文源代碼下載:https://github.com/ityouknow/spring-boot-examples