SpringBoot彩蛋之定制启动画面
在Spring Boot应用中,可以通过实现Banner
接口或者覆盖SpringBootServletInitializer
类来定制启动画面。
以下是一个简单的实例,展示如何通过覆盖SpringApplicationBuilder
来定制启动画面:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
public class CustomBannerApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(CustomBannerApplication.class)
.banner(new CustomBanner());
}
public static void main(String[] args) {
SpringApplication.run(CustomBannerApplication.class, args);
}
static class CustomBanner implements Banner {
@Override
public void printBanner(Environment environment, Class<?> sourceClass, PrintStream out) {
out.println(""); // 空白或者自定义的启动banner信息
out.println(" _oo _ _ ");
out.println(" / -_- \\ | |_ _ ._ _| |_ _ ._ _ ");
out.println(" / | | | | | | | | | | | | | | | | | | ");
out.println(" \\ \\V / | |_| | | |_| | |_| | |_| |_|_| ");
out.println(" \\_|_|_/ |_| |_| |_| |_| |_| |_/ ");
out.println("");
}
}
}
在上述代码中,CustomBannerApplication
类覆盖了SpringBootServletInitializer
并在configure
方法中通过banner
方法设置了一个自定义的Banner
实现CustomBanner
。当应用启动时,Spring Boot将会打印出这个自定义的banner而不是默认的banner。
评论已关闭