title: SpringBoot之Banner tags:
- SpringBoot
- banner categories: springboot date: 2017-11-14 22:48:02
背景
初次运行SpringBoot的小伙伴想必对于SpringBoot打印的Banner很感兴趣
通常长成这样
一个Spring扑面而来~
那么我们能否定制自己的启动页呢?
源码
SpringBoot在启动时会调用如下一段代码
private Banner printBanner(ConfigurableEnvironment environment) {
if (this.bannerMode == Banner.Mode.OFF) {
return null;
}
ResourceLoader resourceLoader = this.resourceLoader != null ? this.resourceLoader
: new DefaultResourceLoader(getClassLoader());
SpringApplicationBannerPrinter bannerPrinter = new SpringApplicationBannerPrinter(
resourceLoader, this.banner);
if (this.bannerMode == Mode.LOG) {
return bannerPrinter.print(environment, this.mainApplicationClass, logger);
}
return bannerPrinter.print(environment, this.mainApplicationClass, System.out);
}
public Banner print(Environment environment, Class<?> sourceClass, PrintStream out) {
Banner banner = getBanner(environment, this.fallbackBanner);
banner.printBanner(environment, sourceClass, out);
return new PrintedBanner(banner, sourceClass);
}
private Banner getBanner(Environment environment, Banner definedBanner) {
Banners banners = new Banners();
banners.addIfNotNull(getImageBanner(environment));
banners.addIfNotNull(getTextBanner(environment));
if (banners.hasAtLeastOneBanner()) {
return banners;
}
if (this.fallbackBanner != null) {
return this.fallbackBanner;
}
return DEFAULT_BANNER;
}
private static final Banner DEFAULT_BANNER = new SpringBootBanner();
复制代码
而 SpringBootBanner长成这样
/**
* Default Banner implementation which writes the 'Spring' banner.
*
* @author Phillip Webb
*/
class SpringBootBanner implements Banner {
private static final String[] BANNER = { "",
" . ____ _ __ _ _",
" /\\\\ / ___'_ __ _ _(_)_ __ __ _ \\ \\ \\ \\",
"( ( )\\___ | '_ | '_| | '_ \\/ _` | \\ \\ \\ \\",
" \\\\/ ___)| |_)| | | | | || (_| | ) ) ) )",
" ' |____| .__|_| |_|_| |_\\__, | / / / /",
" =========|_|==============|___/=/_/_/_/" };
private static final String SPRING_BOOT = " :: Spring Boot :: ";
private static final int STRAP_LINE_SIZE = 42;
@Override
public void printBanner(Environment environment, Class<?> sourceClass,
PrintStream printStream) {
for (String line : BANNER) {
printStream.println(line);
}
String version = SpringBootVersion.getVersion();
version = (version == null ? "" : " (v" + version + ")");
String padding = "";
while (padding.length() < STRAP_LINE_SIZE
- (version.length() + SPRING_BOOT.length())) {
padding += " ";
}
printStream.println(AnsiOutput.toString(AnsiColor.GREEN, SPRING_BOOT,
AnsiColor.DEFAULT, padding, AnsiStyle.FAINT, version));
printStream.println();
}
}
复制代码
上述代码可以看到banner存在两种 imageBanner和textBanner
imageBanner需要配置属性为banner.image.location
或者支持名为banner格式为 "gif", "jpg", "png的图片
textBanner支持默认情况下在banner.location 或者默认为banner.txt
比如我们可以如下设置
在resource分别放置banner.txt和banner.png
banner.png
banner.txt
/$$$$$$$$ /$$$$$$
| $$_____//$$__ $$
| $$ | $$ \__/
| $$$$$ | $$$$$$$
| $$__/ | $$__ $$
| $$ | $$ \ $$
| $$ | $$$$$$/
|__/ \______/
复制代码
我们可以看一下结果
哈哈 F6小人变成了这样!!!
生成文字banner可以使用 patorjk.com/software/ta… 【同样可以用在javascript 打印出console~】