SpringBoot(1) - SpringApplication(1) - 自定义Banner

本文介绍如何在Spring Boot项目中自定义启动时显示的Banner,包括文本和图片格式,以及如何利用Banner变量显示项目和Spring Boot版本信息。同时,讲解了如何通过配置控制Banner的输出方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考:https://docs.spring.io/spring-boot/docs/1.5.17.RELEASE/reference/htmlsingle/#boot-features-banner

 

  • Banner在项目启动时打印,我们可以在classpath下添加banner.txt改变默认的Banner,或者通过banner.location属性指定banner.txt的位置。
  • 假如文件不是常见的编码,通过设置banner.charset(默认UTF-8)
  • 除了可以添加文本文件,也可以在classpath下添加banner.gif,banner.jpg或者banner.png等图片文件,或者设置banner.image.location属性。图片会被转换成ASCII形式展示。

 

1. Banner变量

变量描述
${application.version}项目定义在MANIFEST.MF文件中的版本信息。例如Implementation-Version: 1.0,即打印1.0
${application.formatted-version}格式化项目定义在MANIFEST.MF文件中的版本信息。如(v1.0)
${spring-boot.version}使用的SpringBoot版本,例如1.5.17.RELEASE
${spring-boot.formatted-version}格式化使用的SpringBoot版本,如(v1.5.17.RELEASE)
${Ansi.NAME}(或者${AnsiColor.NAME}${AnsiBackground.NAME}${AnsiStyle.NAME}NAME是ANSI转义码的名称,参考AnsiPropertySource.java
${application.title}项目定义在MANIFEST.MF文件中的标题。例如Implementation-Title: MyApp,即打印MyAPP

2. 控制banner输出方式

2.1 使用spring.main.banner-mode属性

  • console:控制台打印(System.out)
  • log:日志文件打印
  • off:关闭Banner,既不在console也不在log中打印

application.yml配置关闭Banner,off必须加上双引号

spring:
  main:
    banner-mode: "off"

2.2 程序中设置

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(Application.class);
        // 关闭Banner,可选值CONSOLE、LOG、OFF
        app.setBannerMode(Banner.Mode.OFF);
        app.run(Application.class, args);
    }
}

3. Banner.txt

Banner.txt放到src/main/resources下(即classpath目录下),也可以设置控制台输出颜色,内容如下:

   _____   ________    ________
  /     \  \_____  \  /  _____/
 /  \ /  \  /  / \  \/   \  ___
/    Y    \/   \_/.  \    \_\  \
\____|__  /\_____\ \_/\______  /
        \/        \__>       \/
MQG SpringBoot${spring-boot.formatted-version}

启动时控制台打印:

4. 生成ascii的网站

http://www.network-science.de/ascii/

 

### Spring Boot 自定义 Banner 不显示的解决方案 Spring Boot 提供了多种方式来设置自定义 Banner,但如果 Banner 未能正确显示,可能是由于配置错误或某些细节未被正确处理。以下是解决此问题的详细分析和方法。 #### 1. 检查 Banner 文件路径 确保 `banner.txt` 文件位于正确的目录下。Spring Boot 默认会在 `src/main/resources/` 目录下查找 `banner.txt` 文件。如果文件路径不正确,Banner 将不会显示[^4]。 ```plaintext src/main/resources/banner.txt ``` 如果需要指定其他位置,可以在 `application.yml` 或 `application.properties` 中进行配置: ```yaml spring: banner: location: classpath:custom-banner.txt ``` 或者在 `application.properties` 中: ```properties spring.banner.location=classpath:custom-banner.txt ``` #### 2. 检查 Banner Mode 配置 如果 Banner 模式被显式设置为 `OFF`,则 Banner 不会显示。请检查启动代码中是否调用了以下方法: ```java springApplication.setBannerMode(Banner.Mode.OFF); ``` 如果不需要关闭 Banner,请将模式设置为 `CONSOLE` 或 `LOG`: ```java springApplication.setBannerMode(Banner.Mode.CONSOLE); ``` 此外,也可以通过配置文件禁用 Banner: ```yaml spring: main: banner-mode: "off" ``` 或者在 `application.properties` 中: ```properties spring.main.banner-mode=off ``` 如果 Banner 不显示,可能是因为 Banner 模式被误设为 `OFF`[^1]。 #### 3. 自定义 Banner 实现类 如果直接使用 `banner.txt` 文件无法解决问题,可以尝试实现自定义的 `Banner` 接口。以下是一个示例: ```java 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("Welcome to My Custom Banner!"); out.println("This is a custom banner implementation."); } } ``` 然后在启动类中注册该 Banner: ```java @SpringBootApplication public class SpringBootStudy002Application { public static void main(String[] args) { SpringApplication springApplication = new SpringApplication(SpringBootStudy002Application.class); springApplication.setBanner(new CustomBanner()); springApplication.run(args); } } ``` 这种方式可以完全控制 Banner 的内容和格式[^3]。 #### 4. 检查 Spring Boot 版本兼容性 某些 Banner 配置可能与特定版本的 Spring Boot 不兼容。请确保使用的 Spring Boot 版本支持所配置的功能。例如,较旧的 Spring Boot 版本可能不支持某些 Banner 配置选项[^2]。 #### 5. 调试 Banner 加载逻辑 如果以上方法均无效,可以通过调试源码来定位问题。Spring Boot 的 Banner 加载逻辑位于 `SpringApplication` 类中,具体方法为 `printBanner()`。可以在此方法中添加断点,检查 Banner 是否被正确加载[^3]。 --- ### 示例代码 以下是一个完整的示例,展示如何通过配置文件和代码实现自定义 Banner: ```java package org.cc11001100; import org.springframework.boot.Banner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringBootStudy002Application { public static void main(String[] args) { SpringApplication springApplication = new SpringApplication(SpringBootStudy002Application.class); springApplication.setBannerMode(Banner.Mode.CONSOLE); // 确保 Banner 模式为 CONSOLE springApplication.run(args); } } ``` 同时,在 `src/main/resources/` 目录下创建 `banner.txt` 文件,内容如下: ```plaintext _______ __ __ _______ | | | | | | | | |_______| | |_| | |_______| _______ __ __ _______ | | | | | | | | |_______| | |_| | |_______| ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值