Spring Boot 自定义启动 Banner(附艺术字体使用指南)

Spring Boot 自定义启动 Banner(附艺术字体使用指南)

Spring Boot 的启动 Banner 是应用启动时显示在控制台的 ASCII 艺术图形,通过自定义 Banner 可以增加应用的个性化和专业感。下面详细介绍如何创建和使用自定义 Banner,包括艺术字体的生成方法。

一、基础自定义方法

1. 使用文本文件创建 Banner

步骤:

  1. src/main/resources 目录下创建 banner.txt 文件
  2. 在文件中添加自定义内容
  3. 启动应用查看效果

示例 banner.txt:

  ___ _ __  _ __(_) ___| |__  
 / __| '_ \| '__| |/ __| '_ \ 
| (__| |_) | |  | | (__| | | |
 \___| .__/|_|  |_|\___|_| |_|
     |_|   :: Spring Boot ::

2. 使用图片创建 Banner

Spring Boot 支持将图片转换为 ASCII 艺术字:

  1. src/main/resources 放置图片文件(banner.gif, banner.jpg, 或 banner.png)
  2. 配置图片转换参数:
# application.properties
spring.banner.image.location=banner.png
spring.banner.image.width=70
spring.banner.image.height=20
spring.banner.image.invert=false  # 是否反转颜色

二、高级自定义技巧

1. 使用动态变量

在 banner.txt 中使用 Spring Boot 提供的变量:

${AnsiColor.BRIGHT_CYAN}
   ____                              
  / __ \____ _____  ____ ____  _____
 / / / / __ `/ __ \/ __ `/ _ \/ ___/
/ /_/ / /_/ / / / / /_/ /  __/ /    
\____/\__,_/_/ /_/\__, /\___/_/     
                 /____/             
${AnsiColor.BRIGHT_YELLOW}
:: ${application.title} ${application.version} ::
:: Spring Boot ${spring-boot.version} ::
:: Java ${java.version} ::

可用变量:

  • ${application.version} - MANIFEST.MF 中定义的版本
  • ${application.formatted-version} - 格式化后的版本
  • ${spring-boot.version} - Spring Boot 版本
  • ${AnsiColor.NAME} - ANSI 颜色代码
  • ${AnsiBackground.NAME} - ANSI 背景色
  • ${AnsiStyle.NAME} - ANSI 样式(BOLD, ITALIC 等)

2. ANSI 颜色与样式

颜色示例:

${AnsiColor.BRIGHT_RED}RED TEXT
${AnsiColor.GREEN}GREEN TEXT
${AnsiBackground.BLUE}BLUE BACKGROUND
${AnsiStyle.UNDERLINE}UNDERLINED TEXT

完整颜色列表:

  • BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE
  • BRIGHT_BLACK, BRIGHT_RED, BRIGHT_GREEN, BRIGHT_YELLOW,
    BRIGHT_BLUE, BRIGHT_MAGENTA, BRIGHT_CYAN, BRIGHT_WHITE

三、艺术字体生成指南

1. 在线生成工具

推荐工具:

  1. patorjk.com/taag - 经典 ASCII 艺术字生成器
  2. www.bootschool.net/ascii - Spring Boot 专用生成器
  3. manytools.org/hacker-tools/ascii-banner - 多风格生成器

使用步骤:

  1. 访问任意生成器网站
  2. 输入文本内容
  3. 选择字体风格(推荐:Big, Ogre, Standard)
  4. 调整参数(宽度、高度)
  5. 复制生成的 ASCII 艺术字
  6. 粘贴到 banner.txt 中

2. 使用 Figlet 命令行工具

安装 Figlet (Linux/macOS):

# Debian/Ubuntu
sudo apt-get install figlet

# macOS
brew install figlet

生成艺术字:

figlet -f slant "My App" > banner.txt

可用字体:

  • 查看所有字体:showfigfonts
  • 常用字体:block, script, slant, small, standard

3. 使用 Java 库生成

在代码中动态生成 Banner:

import com.github.lalyos.jfiglet.FigletFont;

public class BannerGenerator {
    public static void main(String[] args) throws Exception {
        String asciiArt = FigletFont.convertOneLine("Spring Boot");
        System.out.println(asciiArt);
    }
}

Maven 依赖:

<dependency>
    <groupId>com.github.lalyos</groupId>
    <artifactId>jfiglet</artifactId>
    <version>0.0.8</version>
</dependency>

四、专业 Banner 示例

1. 企业级 Banner

${AnsiColor.BRIGHT_BLUE}
███████╗██████╗ ██╗███╗   ██╗ ██████╗     ██████╗ ██████╗  ██████╗ ████████╗
██╔════╝██╔══██╗██║████╗  ██║██╔════╝     ██╔══██╗██╔══██╗██╔═══██╗╚══██╔══╝
███████╗██████╔╝██║██╔██╗ ██║██║  ███╗    ██████╔╝██████╔╝██║   ██║   ██║   
╚════██║██╔═══╝ ██║██║╚██╗██║██║   ██║    ██╔══██╗██╔══██╗██║   ██║   ██║   
███████║██║     ██║██║ ╚████║╚██████╔╝    ██████╔╝██║  ██║╚██████╔╝   ██║   
╚══════╝╚═╝     ╚═╝╚═╝  ╚═══╝ ╚═════╝     ╚═════╝ ╚═╝  ╚═╝ ╚═════╝    ╚═╝   

${AnsiColor.BRIGHT_CYAN}
:: ${application.title} :: (v${application.version}) ::
:: Spring Boot ${spring-boot.version} :: Profile: ${spring.profiles.active} ::
:: Server: ${server.port} :: Database: ${spring.datasource.url} ::
${AnsiColor.DEFAULT}

2. 极简风格 Banner

${AnsiStyle.BOLD}${AnsiColor.BRIGHT_GREEN}
┌──────────────────────────────────────────────────────┐
│   ██████╗ ██████╗ ██╗   ██╗██████╗ ███████╗██████╗  │
│  ██╔════╝██╔═══██╗██║   ██║██╔══██╗██╔════╝██╔══██╗ │
│  ██║     ██║   ██║██║   ██║██████╔╝█████╗  ██████╔╝ │
│  ██║     ██║   ██║██║   ██║██╔══██╗██╔══╝  ██╔══██╗ │
│  ╚██████╗╚██████╔╝╚██████╔╝██║  ██║███████╗██║  ██║ │
│   ╚═════╝ ╚═════╝  ╚═════╝ ╚═╝  ╚═╝╚══════╝╚═╝  ╚═╝ │
└──────────────────────────────────────────────────────┘
${AnsiColor.RESET}

3. 动态环境信息 Banner

${AnsiColor.BRIGHT_MAGENTA}
  _____             __  __   ______          __ 
 / ___/____  ____  / /_/ /  /_  __/__  _____/ /_
 \__ \/ __ \/ __ \/ __/ /    / / / _ \/ ___/ __/
 ___/ / /_/ / /_/ / /_/ /    / / /  __(__  ) /_  
/____/\____/\____/\__/_/    /_/  \___/____/\__/  

${AnsiColor.BRIGHT_CYAN}
> Environment: ${spring.profiles.active}
> Active DB: ${spring.datasource.url}
> Server: ${server.address}:${server.port}
> Java: ${java.version} (${java.vendor})
> Spring Boot: ${spring-boot.version}
${AnsiColor.DEFAULT}

五、高级编程配置

1. 完全自定义 Banner 实现

import org.springframework.boot.Banner;
import org.springframework.core.env.Environment;
import java.io.PrintStream;

public class CustomBanner implements Banner {

    @Override
    public void printBanner(Environment environment, 
                            Class<?> sourceClass, 
                            PrintStream out) {
        out.println();
        out.println("  ███████╗ ██████╗ █████╗ ███╗   ██╗");
        out.println("  ██╔════╝██╔════╝██╔══██╗████╗  ██║");
        out.println("  ███████╗██║     ███████║██╔██╗ ██║");
        out.println("  ╚════██║██║     ██╔══██║██║╚██╗██║");
        out.println("  ███████║╚██████╗██║  ██║██║ ╚████║");
        out.println("  ╚══════╝ ╚═════╝╚═╝  ╚═╝╚═╝  ╚═══╝");
        out.println();
        out.println(" :: Spring Boot :: " + 
                     environment.getProperty("spring-boot.version"));
        out.println(" :: Application :: " + 
                     environment.getProperty("spring.application.name"));
    }
}

2. 在启动类中应用自定义 Banner

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

@SpringBootApplication
public class MyApplication {
    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(MyApplication.class);
        
        // 设置自定义 Banner
        app.setBanner(new CustomBanner());
        
        // 或使用模式设置
        // app.setBannerMode(Banner.Mode.CONSOLE); // 默认
        // app.setBannerMode(Banner.Mode.LOG);     // 输出到日志
        // app.setBannerMode(Banner.Mode.OFF);     // 关闭
        
        app.run(args);
    }
}

六、艺术字体设计技巧

1. 设计原则

  • 尺寸:宽度建议在 70-80 字符以内,高度 5-10 行
  • 对比度:使用不同 ANSI 颜色增强可读性
  • 留白:在 Banner 周围保留空白区域
  • 信息分层:主标题 + 副标题 + 环境信息

2. 常用字体风格

字体名称特点适用场景
Standard经典等宽通用场景
Big大型粗体标题突出
Slant斜体效果现代应用
Script手写风格艺术类应用
Digital电子风格科技类应用
Ogre哥特风格游戏应用

3. 多行文本布局技巧

${AnsiColor.BRIGHT_YELLOW}
  ┌──────────────────────────────────────┐
  │   ██████╗ ██████╗ ██╗   ██╗██████╗  │
  │  ██╔════╝██╔═══██╗██║   ██║██╔══██╗ │
  │  ██║     ██║   ██║██║   ██║██████╔╝ │
  │  ██║     ██║   ██║██║   ██║██╔══██╗ │
  │  ╚██████╗╚██████╔╝╚██████╔╝██║  ██║ │
  │   ╚═════╝ ╚═════╝  ╚═════╝ ╚═╝  ╚═╝ │
  └──────────────────────────────────────┘
${AnsiColor.BRIGHT_CYAN}
  > Environment: ${spring.profiles.active}
  > Active DB: ${spring.datasource.url}
  > Server: ${server.address}:${server.port}
${AnsiColor.RESET}

七、常见问题解决

1. Banner 不显示

  • 检查 banner.txt 是否在 src/main/resources 目录
  • 确认没有设置 spring.main.banner-mode=off
  • 检查文件名是否正确(banner.txt 或 banner.png)

2. ANSI 颜色不生效

  • Windows 需要启用 ANSI 支持:
    • application.properties 中添加:
      spring.output.ansi.enabled=ALWAYS
      
    • 对于旧版 Windows,使用 JANSI 库:
      <dependency>
          <groupId>org.fusesource.jansi</groupId>
          <artifactId>jansi</artifactId>
          <version>2.4.0</version>
      </dependency>
      

3. 图片转换效果差

  • 调整图片尺寸:spring.banner.image.width=70
  • 尝试不同图片格式(PNG 效果最佳)
  • 使用高对比度简单图片

4. 特殊字符显示异常

  • 确保 IDE 或终端使用 UTF-8 编码
  • application.properties 中设置:
    spring.banner.charset=UTF-8
    

八、最佳实践建议

  1. 环境区分:为开发和生产环境设置不同 Banner

    ${AnsiColor.BRIGHT_RED}
    ${AnsiStyle.BLINK}
    !!! DEVELOPMENT ENVIRONMENT !!!
    ${AnsiStyle.NORMAL}
    ${AnsiColor.RESET}
    [主 Banner 内容]
    
  2. 安全考虑:避免在 Banner 中暴露敏感信息

  3. 性能优化:复杂 Banner 只用于开发环境

  4. 版本信息:始终包含应用版本和 Spring Boot 版本

  5. 响应式支持:为 WebFlux 应用添加特殊标识

  6. 多模块项目:为每个模块设置独特 Banner

通过精心设计的自定义 Banner,你可以在应用启动时展示专业形象,同时提供关键环境信息,增强开发体验和团队认同感。艺术字体的使用更可以提升视觉吸引力,让 Spring Boot 应用的启动过程更加个性化和专业化。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值