SpringBoot自定义Banner

可以通过下面方法修改Banner

  1. classpath路径增加banner.txt文件
  2. 配置spring.banner.location属性
  3. classpath路径增加banner.gif,banner.jpg或者banner.png文件
  4. 配置spring.banner.image.location属性

在banner.txt文件可以配置的占位符如下

  • ${application.version}
  • ${application.formatted-version}
  • ${spring-boot.version}
  • ${spring-boot.formatted-version}
  • ${Ansi.NAME} (or ${AnsiColor.NAME}, ${AnsiBackground.NAME}, ${AnsiStyle.NAME})
  • ${application.title}

增加banner.txt示例

在工程的resources目录中增加banner.txt文件
添加如下代码

#####################################################################
# This is demo
# spring-boot.version:${spring-boot.version}
#####################################################################
b1.png

自定义Banner实现类

可以通过实现如下接口来实现
org.springframework.boot.Banner

@FunctionalInterface
public interface Banner {

    /**
     * Print the banner to the specified print stream.
     * @param environment the spring environment
     * @param sourceClass the source class for the application
     * @param out the output print stream
     */
    void printBanner(Environment environment, Class<?> sourceClass, PrintStream out);

示例代码

@SpringBootApplication
public class SpringbootApplication {

    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(SpringbootApplication.class);
        DemoBanner banner=new DemoBanner();
        app.setBanner(banner);
        app.run(args);
    }
}
public class DemoBanner  implements Banner {
    @Override
    public void printBanner(Environment environment, Class<?> sourceClass, PrintStream out) {
        out.println("DemoBanner ===>> banner");
    }
}
b2.png

Banner输出模式

spring.main.banner-mode
console :控制台
log:日志文件
off:不输出

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。