SpringBoot自定义启动banner:给项目加个专属“开机画面”

SpringBoot自定义启动banner:给项目加个专属"开机画面"

刚学SpringBoot的时候,每次启动项目,控制台都会跳出默认的SpringBoot logo和版本信息,看久了总觉得少点意思。后来发现原来这个启动画面(也就是banner)是可以自定义的,花几分钟改一改,既能加项目名称、版本,甚至还能加些有趣的字符画,瞬间让自己的项目有了专属感。今天就聊聊怎么自定义SpringBoot的启动banner,步骤超简单,新手也能跟着做。

先说说默认的banner长啥样,启动SpringBoot项目时,控制台会输出这样的内容:

复制代码
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.7.0)

这个就是默认banner,我们要做的就是替换掉它,换成自己想要的内容。

最简单的方式:自定义txt文件

SpringBoot默认会读取resources目录下的banner.txt文件作为启动画面,只要创建这个文件,写点自己的内容,启动项目时就会自动替换默认banner。

步骤1:创建banner.txt文件

在项目的src/main/resources目录下,新建一个名为banner.txt的文件(名字必须是banner.txt,不然SpringBoot识别不到)。

步骤2:写自定义内容

可以直接写文字,比如项目名称、版本、开发者信息,也可以加些字符画。比如我写了这样的内容:

复制代码
===============================
  项目名称:MySpringBootDemo
  版本号:v1.0.0
  开发者:自己
===============================
  ██████  ██████  ███    ██ ██    ██ ███████
 ██      ██    ██ ████   ██ ██    ██ ██
 ██      ██    ██ ██ ██  ██ ██    ██ ███████
 ██      ██    ██ ██  ██ ██  ██  ██      ██
  ██████  ██████  ██   ████   ████   ███████
===============================
:: Spring Boot :: (v${spring-boot.version})

这里有几个小技巧:

  • ${spring-boot.version}:可以自动获取当前SpringBoot的版本号,不用手动写死;
  • ${AnsiColor.BRIGHT_GREEN}:设置文字颜色,比如亮绿色,控制台显示更醒目;

步骤3:启动项目看效果

启动SpringBoot项目,控制台就会输出我们写的banner内容,不再是默认的SpringBoot logo了。

进阶玩法:用字符画生成器

如果想搞点有趣的字符画,不用自己手动敲,网上有很多字符画生成工具,比如:

比如我想把"MYPROJECT"转成字符画,在taag工具里选个字体,生成后复制到banner.txt里就行。生成的字符画示例:

复制代码
  __  __     ______     ______     __     __    
 /\ \/ /    /\  ___\   /\  __ \   /\ \   /\ \   
 \ \  _"-.  \ \  __\   \ \  __ \  \ \ \  \ \ \  
  \ \_\ \_\  \ \_____\  \ \_\ \_\  \ \_\  \ \_\ 
   \/_/\/_/   \/_____/   \/_/\/_/   \/_/   \/_/ 

把这段复制到banner.txt,启动项目就能看到专属的字符画banner了。

额外技巧:控制banner的显示和位置

1. 关闭banner

如果不想显示任何banner,也很简单,有两种方式:

方式1:配置文件关闭

application.properties里加一行配置:

properties 复制代码
spring.main.banner-mode=off

或者在application.yml里:

yaml 复制代码
spring:
  main:
    banner-mode: off
方式2:代码里关闭

在启动类的main方法里,用SpringApplicationBuilder设置:

java 复制代码
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;

@SpringBootApplication
public class MySpringBootDemoApplication {
    public static void main(String[] args) {
        // 关闭banner
        new SpringApplicationBuilder(MySpringBootDemoApplication.class)
                .bannerMode(org.springframework.boot.Banner.Mode.OFF)
                .run(args);
        
        // 也可以用简洁写法:
        // SpringApplication app = new SpringApplication(MySpringBootDemoApplication.class);
        // app.setBannerMode(Banner.Mode.OFF);
        // app.run(args);
    }
}

2. 自定义banner文件的位置和名称

如果不想把文件叫banner.txt,或者想放在其他目录,比如resources/banner/my-banner.txt,可以在配置文件里指定路径:

properties 复制代码
# 指定自定义banner文件的位置
spring.banner.location=classpath:banner/my-banner.txt

也可以用图片作为banner(比如banner.png、banner.jpg),SpringBoot会自动把图片转成字符画显示,不过效果不如纯文本的好,感兴趣的可以试试。

3. 用代码动态生成banner

如果想更灵活,比如根据环境(开发/生产)显示不同的banner,还能通过代码动态生成。创建一个实现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) {
        // 动态输出banner内容
        out.println("================================");
        out.println("  环境:" + environment.getProperty("spring.profiles.active"));
        out.println("  项目启动成功!");
        out.println("================================");
    }
}

然后在启动类里指定使用这个自定义banner:

java 复制代码
@SpringBootApplication
public class MySpringBootDemoApplication {
    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(MySpringBootDemoApplication.class);
        // 使用自定义的Banner类
        app.setBanner(new CustomBanner());
        app.run(args);
    }
}

这样启动时就会输出我们动态生成的内容,还能读取配置文件里的环境变量,适配不同环境的显示需求。

其实自定义banner就是个小技巧,不影响项目功能,但能让自己的项目更有辨识度。我平时做个人项目时,都会加个简单的自定义banner,启动时看到自己写的内容,还挺有成就感的。而且步骤特别简单,不管是写文字、字符画,还是动态生成,几分钟就能搞定,新手也能轻松上手。

相关推荐
假女吖☌2 小时前
限流算法-redis实现与java实现
java·redis·算法
lixin5565562 小时前
基于迁移学习的图像风格增强器
java·人工智能·pytorch·python·深度学习·语言模型
面汤放盐2 小时前
企业权限--系统性方案探究
java·开发语言
what丶k2 小时前
深度解析Redis LRU与LFU算法:区别、实现与选型
java·redis·后端·缓存
悟能不能悟2 小时前
java Date转换为string
java·开发语言
菜宾2 小时前
java-redis面试题
java·开发语言·redis
猿小羽2 小时前
AI 学习与实战系列:Spring AI + MCP 深度实战——构建标准化、可扩展的智能 Agent 系统
java·spring boot·llm·agent·spring ai·mcp·model context protocol
木井巳3 小时前
【递归算法】快速幂解决 pow(x,n)
java·算法·leetcode·深度优先
测试人社区-浩辰3 小时前
AI与区块链结合的测试验证方法
大数据·人工智能·分布式·后端·opencv·自动化·区块链