最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot項目中自定義Banner的技術(shù)指南

 更新時間:2025年03月04日 09:26:12   作者:拾荒的小海螺  
在 Spring Boot 項目中,當(dāng)應(yīng)用啟動時會顯示默認(rèn)的 Spring 標(biāo)志和版本信息,定制化的啟動 Banner 不僅可以美化應(yīng)用,甚至可以提供一些關(guān)鍵信息,本文將介紹如何在 Spring Boot 項目中自定義啟動 Banner,需要的朋友可以參考下

1、簡述

在 Spring Boot 項目中,當(dāng)應(yīng)用啟動時會顯示默認(rèn)的 Spring 標(biāo)志和版本信息。定制化的啟動 Banner 不僅可以美化應(yīng)用,還能在項目中增加個性化的品牌印記,甚至可以提供一些關(guān)鍵信息。本文將介紹如何在 Spring Boot 項目中自定義啟動 Banner,以及如何使用工具生成自定義文本和圖像 Banner。

在這里插入圖片描述

2、Spring Boot 默認(rèn) Banner

Spring Boot 默認(rèn)的 Banner 位于 org/springframework/boot 包內(nèi)的 spring-boot.jar 中。默認(rèn) Banner 只顯示了 Spring 標(biāo)志、版本和應(yīng)用名稱信息,效果如下:

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

3、自定義 Banner

Spring Boot 支持三種方式來定制 Banner:

  • 文本文件(ASCII Art):通過 banner.txt 文件設(shè)置 ASCII Art。
  • 圖片文件:通過 banner.jpg、banner.png 或 banner.gif 設(shè)置圖片。
  • 編程方式:通過編寫 Java 代碼自定義 Banner。

3.1 使用 banner.txt 定義 ASCII Art

最簡單的方法是創(chuàng)建一個 banner.txt 文件并放置在 src/main/resources 目錄中。啟動應(yīng)用時,Spring Boot 會自動加載該文件內(nèi)容并替代默認(rèn) Banner。

實踐步驟:

  • 在 src/main/resources 中創(chuàng)建 banner.txt 文件。
  • 編寫 ASCII 文本圖案。可以使用 Banner Generator 等工具生成自定義字符。

例如,將生成的 Banner 粘貼到 banner.txt 中:

  ____            _         _   ____             _      
 | __ ) _   _ ___| |_ _   _| | | __ )  __ _  ___| | __  
 |  _ \| | | / __| __| | | | | |  _ \ / _` |/ __| |/ /  
 | |_) | |_| \__ \ |_| |_| | | | |_) | (_| | (__|   <   
 |____/ \__,_|___/\__|\__,_|_| |____/ \__,_|\___|_|\_\  

在 Spring Boot 啟動時,將顯示此自定義 ASCII 文本。

3.2 使用圖片作為 Banner

Spring Boot 支持使用圖片作為 Banner。圖片文件的格式可以是 PNG、GIF 或 JPG,系統(tǒng)會在啟動時將圖片渲染成 ASCII 格式。具體支持的圖片格式為黑白圖像。

實踐步驟:

  • 將 banner.png、banner.jpg 或 banner.gif 圖片文件放在 src/main/resources 中。
  • 啟動項目,Spring Boot 會自動加載并顯示圖片的 ASCII 版本。

圖片生成推薦工具

可以使用 Image to ASCII 這類工具生成黑白圖片,將其導(dǎo)出為 PNG 或 JPG 格式,并命名為 banner.png。

3.3 通過編程方式自定義 Banner

如果希望在啟動過程中動態(tài)設(shè)置 Banner,還可以通過 Java 代碼實現(xiàn)。實現(xiàn) Banner 接口來自定義 Banner,并將其注冊到 Spring Boot 應(yīng)用中。

創(chuàng)建一個實現(xiàn) org.springframework.boot.Banner 接口的類:

package com.example.demo;

import org.springframework.boot.Banner;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.io.PrintStream;

@Component
public class CustomBanner implements Banner {
    @Override
    public void printBanner(Environment environment, Class<?> sourceClass, PrintStream out) {
        out.println("=================================");
        out.println("   Welcome to My Spring Boot App ");
        out.println("=================================");
    }
}

在 SpringApplication 中設(shè)置自定義 Banner:

import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(DemoApplication.class);
        app.setBanner(new CustomBanner());
        app.run(args);
    }
}

在啟動應(yīng)用時,將會顯示 CustomBanner 中的內(nèi)容。

4、配置 Banner

Spring Boot 允許在 application.properties 文件中設(shè)置 Banner 的顯示位置和字體顏色。

4.1 設(shè)置 Banner 顯示模式

可以通過 spring.main.banner-mode 屬性來設(shè)置 Banner 的顯示模式:

spring.main.banner-mode=console
  • console:在控制臺打印 Banner(默認(rèn)值)。
  • log:將 Banner 打印到日志文件。
  • off:禁用 Banner。

4.2 設(shè)置字體顏色

要為 Banner 設(shè)置顏色,可以通過 ANSI 轉(zhuǎn)義代碼自定義字體顏色。例如:

${<!--{cke_protected}{C}%3C!%2D%2D%20%2D%2D%3E-->AnsiColor.RED}Welcome to My Spring Boot App${<!--{cke_protected}{C}%3C!%2D%2D%20%2D%2D%3E-->AnsiColor.DEFAULT}

將這段內(nèi)容寫入 banner.txt,即可在控制臺中顯示紅色的文本。常用顏色代碼:

  • ${AnsiColor.BLACK}
  • ${AnsiColor.RED}
  • ${AnsiColor.GREEN}
  • ${AnsiColor.YELLOW}
  • ${AnsiColor.BLUE}
  • ${AnsiColor.MAGENTA}
  • ${AnsiColor.CYAN}
  • ${AnsiColor.WHITE}

注意: ANSI 顏色在部分控制臺中可能不支持。

5、總結(jié)

以下是在 Spring Boot 項目中自定義 Banner 的完整操作步驟:

  • 使用 ASCII Art 定義 Banner:在 src/main/resources/banner.txt 中添加 ASCII Art 內(nèi)容。
  • 使用圖片作為 Banner:將黑白圖片文件(如 banner.png)放置到 src/main/resources 中。
  • 通過編程方式自定義 Banner:實現(xiàn) Banner 接口,并在 SpringApplication 中注冊。
  • 在 application.properties 中配置:選擇 Banner 顯示模式和字體顏色。

通過本文的方法,您可以為 Spring Boot 項目添加一個有趣的、個性化的啟動畫面,讓您的應(yīng)用更具特色。

到此這篇關(guān)于SpringBoot項目中自定義Banner的技術(shù)指南的文章就介紹到這了,更多相關(guān)SpringBoot自定義Banner內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

长乐市| 犍为县| 武安市| 民县| 石家庄市| 怀远县| 湛江市| 固阳县| 札达县| 安庆市| 邵阳县| 安达市| 林州市| 梓潼县| 察哈| 杭州市| 犍为县| 兴义市| 福贡县| 察雅县| 旬邑县| 大邑县| 大埔县| 和田市| 阿克陶县| 启东市| 彰武县| 鄂伦春自治旗| 三江| 旬阳县| 鲁山县| 巴林右旗| 融水| 罗定市| 霍城县| 荥阳市| 慈利县| 时尚| 岳阳市| 理塘县| 花莲市|