SpringBoot_SpringApplication_自定义banner

本文介绍如何从https://start.spring.io/获取Maven项目模板,并通过Eclipse进行项目导入。文中详细说明了自定义项目启动时显示的Banner的方法,包括在classpath路径下新建banner.txt文件及在application.properties中配置Banner位置。

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

  1. 创建meavn项目 可以从https://start.spring.io/ 获取下载以后导出eclpse
  2. 导入项目的格式
  3. 自定义banner

    1、在classpath路径下新建banner.txt文件

    2、application.properties 文件加入路径:

    ① spring.banner.location  

    ②如果不是UTF-8编码  加入 spring.banner.charset

    ③如果是图片文件 使用 spring.banner.image.location

  4. 新建banner.txt此处使用了码云大神txt
  5. 运行Main函数



### 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、付费专栏及课程。

余额充值