一、什麼是SpringBoot的Banner?
在講解如何自定義SpringBoot的Banner之前,我們需要先了解一下什麼是SpringBoot的Banner。當我們啟動一個SpringBoot應用的時候,控制台上就會有一個默認的Banner顯示。這個Banner通常是Spring Boot的標誌性Logo,它的設計衍生自Pivotal公司的logo。默認的Banner內容如下:
_______ _______ / ___ \ / ___ \ | / \ | __ _ _______ __ _______ | / \ | __ _ _______ | | | |/ _` |_ / _ \| / /_ / _ \ | | | |/ _` |_ / _ \ | \__/ | (_| |/ / (_) | < / / (_) || \__/ | (_| |/ / (_) | \______/ \__,_/___\___/|_|\_\/___\___/ \______/ \__,_/___\___/
這個Banner可以作為SpringBoot應用的一個重要標識,顯示在控制台中。
二、如何自定義Banner?
1、創建Banner的文本文件
首先,我們需要創建Banner的文本文件。這個文本文件可以是任何格式的文本文件,只需要在文件中添加我們自己想顯示的Banner即可。例如,我們可以使用記事本創建一個banner.txt文件,並在文件中添加我們自己的Banner。
_______ _______ _ _ _ | || | | | | | | | |_ _|| _ | ___ | | ___ | |_ | | | | | | | | / _ \ | | / _ \| __| | | | | | |_| || (_) || || __/| |_ |_| |___| |_______| \___/ |_| \___| \__| (_)
注意:自定義Banner的文本中可以包含各種特殊字元,如單位符號,其他語言字元等。你可以使用網上一些生成Banner的網站來生成自定義Banner的文本,並將其保存為文本文件。
2、Banner的載入方式
SpringBoot提供了多種載入Banner的方式:
- 在classpath根目錄下放置banner.txt文件
SpringBoot會在classpath下默認找到banner.txt,並將其作為Banner輸出,如果存在多個banner.txt文件,SpringBoot會隨機選擇一個進行輸出。當然,如果你把文本文件命名為banner.gif、banner.jpg或者banner.png也是可以的,此時SpringBoot會自動把圖片轉換為ASCII字元來顯示Banner。例如,將圖片轉化為Banner的效果如下:
,ooo88888888oo, ,oo` `88 `o, ,88` 88 `88, ,88` 88 `88, ,88` 88 `88, 88` 88 `88 88 88 88 _____ ____ ____ / ____| / __ \ / __ \ | | | | | | | | | | | | | | | | | | | | | |____ | |__| | | |__| | \_____| \____/ \____/
- 在SpringApplication中設置Banner
我們可以在SpringApplication中設置Banner。具體實現是創建一個SpringApplication實例,然後調用setBanner方法來設置Banner,示例代碼如下:
@SpringBootApplication public class CustomBannerDemoApplication { public static void main(String[] args) { SpringApplication app = new SpringApplication(CustomBannerDemoApplication.class); app.setBanner(new MyBanner()); app.run(args); } } class MyBanner implements Banner { @Override public void printBanner(Environment environment, Class<?> aClass, PrintStream printStream) { printStream.print("\n 我的自定義Banner\n\n"); } }
上面的示例代碼中,我們在MyBanner類中實現了Banner介面,並覆蓋了其中的printBanner方法,使用自定義的Banner替代了默認的Banner。
三、自定義Banner的實際應用示例
我們可以使用Thymeleaf模板引擎來創建Banner文本,並且將其輸出到控制台中。 大致步驟如下:
- 在pom.xml文件中添加thymeleaf的依賴,如下所示:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
- 創建Banner的Thymeleaf模板文件:
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Spring Boot Custom Banner</title> </head> <body> <pre th:text="${banner}"> ${banner} </pre> </body> </html>
- 創建BannerWriter類,用於輸出自定義Banner:
public class BannerWriter { private static final String BANNER_LOCATION = "banner.html"; public void write(Environment environment, Class<?> sourceClass, PrintStream out) { try { final ResourceLoader resourceLoader = new DefaultResourceLoader(); final Resource banner = resourceLoader.getResource("classpath:" + BANNER_LOCATION); if (!banner.exists()) { throw new FileNotFoundException(); } try (Reader reader = new InputStreamReader(banner.getInputStream(), StandardCharsets.UTF_8)) { final String bannerContent = FileCopyUtils.copyToString(reader); out.println(bannerContent); } } catch (Exception e) { out.println("### SpringApplication Banner ###"); out.println(); } } }
- 在SpringBootApplication中設置Banner:
@SpringBootApplication public class BannerApplication { public static void main(String[] args) { final SpringApplication app = new SpringApplication(BannerApplication.class); app.setBanner(new Banner() { @Override public void printBanner(Environment environment, Class<?> sourceClass, PrintStream out) { new BannerWriter().write(environment, sourceClass, out); } }); app.run(args); } }
四、總結
本文詳細講解了如何自定義SpringBoot的Banner,並提供了多種實現方式,包括在classpath下放置banner.txt文件、使用SpringApplication的setBanner方法設置Banner等,同時,本文還提供了一個實際應用的示例,演示了如何使用Thymeleaf模板引擎來創建Banner文本,並把Banner輸出到控制台中。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/190038.html