今天就跟大家聊聊有關(guān)Banner怎么在SpringBoot中使用,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
網(wǎng)站建設(shè)哪家好,找成都創(chuàng)新互聯(lián)公司!專(zhuān)注于網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開(kāi)發(fā)、微信小程序開(kāi)發(fā)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶(hù)創(chuàng)新互聯(lián)還提供了嵩縣免費(fèi)建站歡迎大家使用!
Spring Boot在啟動(dòng)項(xiàng)目時(shí),控制臺(tái)會(huì)打印一個(gè)Spring的logo。如果不做任何配置
該信息來(lái)源于SpringBootBanner類(lèi)的靜態(tài)常量BANNER,該屬性是一個(gè)字符串?dāng)?shù)組,不指定任何banner屬性時(shí),控制臺(tái)默認(rèn)輸出該數(shù)組數(shù)據(jù)。我們可以通過(guò)Spring Boot提供的強(qiáng)大配置功能來(lái)改變banner的輸出。
通常長(zhǎng)成這樣
一個(gè)Spring撲面而來(lái)~
那么我們能否定制自己的啟動(dòng)頁(yè)呢?
源碼
SpringBoot在啟動(dòng)時(shí)會(huì)調(diào)用如下一段代碼
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長(zhǎng)成這樣
/** * 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支持默認(rèn)情況下在banner.location 或者默認(rèn)為banner.txt
比如我們可以如下設(shè)置
在resource分別放置banner.txt和banner.png
banner.png
banner.txt
/$$$$$$$$ /$$$$$$
| $$_____//$$__ $$
| $$ | $$ \__/
| $$$$$ | $$$$$$$
| $$__/ | $$__ $$
| $$ | $$ \ $$
| $$ | $$$$$$/
|__/ \______/
看完上述內(nèi)容,你們對(duì)Banner怎么在SpringBoot中使用有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。