- SpringBoot自定义启动banner:给项目加个专属“开机画面”
- 最简单的方式:自定义txt文件
- 步骤1:创建banner.txt文件
- 步骤2:写自定义内容
- 步骤3:启动项目看效果
- 进阶玩法:用字符画生成器
- 额外技巧:控制banner的显示和位置
- 1. 关闭banner
- 方式1:配置文件关闭
- 方式2:代码里关闭
- 2. 自定义banner文件的位置和名称
- 3. 用代码动态生成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了。
进阶玩法:用字符画生成器
如果想搞点有趣的字符画,不用自己手动敲,网上有很多字符画生成工具,比如:
- patorjk.com/software/taag/(可以把文字转成字符画)
- asciiart.eu/(有各种现成的字符画)
比如我想把“MYPROJECT”转成字符画,在taag工具里选个字体,生成后复制到banner.txt里就行。生成的字符画示例:
__ __ ______ ______ __ __ /\ \/ / /\ ___\ /\ __ \ /\ \ /\ \ \ \ _"-. \ \ __\ \ \ __ \ \ \ \ \ \ \ \ \_\ \_\ \ \_____\ \ \_\ \_\ \ \_\ \ \_\ \/_/\/_/ \/_____/ \/_/\/_/ \/_/ \/_/把这段复制到banner.txt,启动项目就能看到专属的字符画banner了。
额外技巧:控制banner的显示和位置
1. 关闭banner
如果不想显示任何banner,也很简单,有两种方式:
方式1:配置文件关闭
在application.properties里加一行配置:
spring.main.banner-mode=off或者在application.yml里:
spring:main:banner-mode:off方式2:代码里关闭
在启动类的main方法里,用SpringApplicationBuilder设置:
importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.boot.builder.SpringApplicationBuilder;@SpringBootApplicationpublicclassMySpringBootDemoApplication{publicstaticvoidmain(String[]args){// 关闭bannernewSpringApplicationBuilder(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,可以在配置文件里指定路径:
# 指定自定义banner文件的位置 spring.banner.location=classpath:banner/my-banner.txt也可以用图片作为banner(比如banner.png、banner.jpg),SpringBoot会自动把图片转成字符画显示,不过效果不如纯文本的好,感兴趣的可以试试。
3. 用代码动态生成banner
如果想更灵活,比如根据环境(开发/生产)显示不同的banner,还能通过代码动态生成。创建一个实现Banner接口的类:
importorg.springframework.boot.Banner;importorg.springframework.core.env.Environment;importjava.io.PrintStream;publicclassCustomBannerimplementsBanner{@OverridepublicvoidprintBanner(Environmentenvironment,Class<?>sourceClass,PrintStreamout){// 动态输出banner内容out.println("================================");out.println(" 环境:"+environment.getProperty("spring.profiles.active"));out.println(" 项目启动成功!");out.println("================================");}}然后在启动类里指定使用这个自定义banner:
@SpringBootApplicationpublicclassMySpringBootDemoApplication{publicstaticvoidmain(String[]args){SpringApplicationapp=newSpringApplication(MySpringBootDemoApplication.class);// 使用自定义的Banner类app.setBanner(newCustomBanner());app.run(args);}}这样启动时就会输出我们动态生成的内容,还能读取配置文件里的环境变量,适配不同环境的显示需求。
其实自定义banner就是个小技巧,不影响项目功能,但能让自己的项目更有辨识度。我平时做个人项目时,都会加个简单的自定义banner,启动时看到自己写的内容,还挺有成就感的。而且步骤特别简单,不管是写文字、字符画,还是动态生成,几分钟就能搞定,新手也能轻松上手。