SpringBoot中banner的配置
SpringBoot中banner的配置
介绍
Spring Boot 提供了自定义启动时的 Banner(横幅)配置,它是在应用程序启动时显示的 ASCII 艺术图案或自定义文本。你可以通过在配置文件中添加一些属性来进行配置。
以下是一些常用的 Spring Boot Banner 配置选项:
- banner.charset:指定 Banner 文件的字符集,默认为 UTF-8。
spring.banner.charset=UTF-8
- banner.location:指定 Banner 文件的路径。可以是文件系统路径(file:)或类路径(classpath:)。如果指定的路径无效,则会回退到默认的 Spring Boot 横幅。
spring.banner.location=classpath:my-banner.txt
- banner.image.location:指定自定义图片横幅的路径。图片横幅可以是 PNG、JPEG 或 GIF 格式的图片文件。
spring.banner.image.location=classpath:my-banner.png
- banner.image.width:指定图片横幅的宽度。默认为 76。
spring.banner.image.width=100
- banner.image.height:指定图片横幅的高度。默认为 -1,表示保持原始高度。
spring.banner.image.height=50
- banner.image.margin:指定图片横幅的边距。默认为 2。
spring.banner.image.margin=5
- banner.text:指定自定义文本横幅的内容。可以使用 ASCII 艺术字体生成器生成艺术字,并将其指定为横幅文本。
spring.banner.text=My Custom Banner
- banner.location.order:指定 Banner 配置的位置查找顺序。默认为
META-INF/spring-banner.txt,classpath:banner.txt
。
spring.banner.location.order=classpath:my-banner.txt,META-INF/spring-banner.txt
以上配置属性可以在 Spring Boot 配置文件(如 application.properties 或 application.yml)中进行设置。你可以根据需要选择适合你应用程序的 Banner 配置方式。
示例
项目结构
项目源码
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
@SpringBootApplication
public class A02SpringBootConfigBannerApplication {
public static void main(String[] args) {
SpringApplicationBuilder builder = new SpringApplicationBuilder(A02SpringBootConfigBannerApplication.class);
builder.bannerMode(Banner.Mode.OFF).run(args);
}
}
.__
________ _______________ ____ _____ ____ ______ ____ | | ____
/ ___/ | \____ \_ __ \_/ __ \ / \_/ __ \\____ \ / _ \| | _/ __ \
\___ \| | / |_> > | \/\ ___/| Y Y \ ___/| |_> > <_> ) |_\ ___/
/____ >____/| __/|__| \___ >__|_| /\___ > __/ \____/|____/\___ >
\/ |__| \/ \/ \/|__| \/