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~】