printBanner
printBanner(environment)メソッドは、org.springframework.context.ConfigurableApplicationContextのコンストラクターで呼び出されます。
if (this.bannerMode == Banner.Mode.OFF) {
return null;
}
ResourceLoader resourceLoader = (this.resourceLoader != null) ? this.resourceLoader
: new DefaultResourceLoader(null);
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);
复制代码
Banner.Mode印刷モード
Banner.Mode列挙クラスは、3つの方法でバナー印刷モードを設定します
/**
* Disable printing of the banner.
不打印
*/
OFF,
/**
* Print the banner to System.out.
打印到控制台
*/
CONSOLE,
/**
* Print the banner to the log file.
打印到日志文件
*/
LOG
复制代码
印刷モードは、構成ファイルを介して設定できます
#不打印
spring.main.banner-mode=off
#打印到控制台
spring.main.banner-mode=console
#打印到日志
spring.main.banner-mode=log
复制代码
printBannerメソッドを振り返る
次に、バナーを印刷するためのSpringApplicationBannerPrinterクラスを作成しました
SpringApplicationBannerPrinter bannerPrinter = new SpringApplicationBannerPrinter(resourceLoader, this.banner);
复制代码
resourceLoaderがデフォルトのリソースローダーになりました
this.banner = null
このSpringApplicationBannerPrinterクラスに初めてアクセスし、静的メンバーのプロパティ値を初期化し、DEFAULT_BANNERを初期化します。
バナーを変更する方法は、バナーインターフェースを実装します
したがって、Bannerクラスを直接作成して、SpringBootBannerを模倣するBannerインターフェイスを実装できます。
public class MyBanner implements Banner {
private static final String[] BANNER = { "", " \n" +
" .::::.\n" +
" .::::::::.\n" +
" :::::::::::\n" +
" ':::::::::::..\n" +
" :::::::::::::::'\n" +
" ':::::::::::.\n" +
" .::::::::::::::'\n" +
" .:::::::::::...\n" +
" ::::::::::::::''\n" +
" .:::. '::::::::''::::\n" +
" .::::::::. ':::::' '::::\n" +
" .::::':::::::. ::::: '::::.\n" +
" .:::::' ':::::::::. ::::: ':::.\n" +
" .:::::' ':::::::::.::::: '::.\n" +
" .::::'' ':::::::::::::: '::.\n" +
" .::'' ':::::::::::: :::...\n" +
" ..:::: ':::::::::' .:' ''''\n" +
" ..''''':' ':::::.' " };
private static final String SPRING_BOOT = " :: fuck :: ";
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 + ")" : "";
StringBuilder padding = new StringBuilder();
while (padding.length() < STRAP_LINE_SIZE - (version.length() + SPRING_BOOT.length())) {
padding.append(" ");
}
printStream.println(AnsiOutput.toString(AnsiColor.GREEN, SPRING_BOOT, AnsiColor.DEFAULT, padding.toString(),
AnsiStyle.FAINT, version));
printStream.println();
}
}
复制代码
メインスタートアップクラスのmainメソッドを変更します
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(AppTest.class);
springApplication.setBanner(new MyBanner());
springApplication.run( args);
}
复制代码
効果:
printBannerメソッドの分析を続行します
org.springframework.boot.SpringApplication#printBanner
buttonPrinter.print(environment、this.mainApplicationClass、System.out)メソッドを呼び出します
3つのパラメーターConfigurableEnvironment構成環境
mainApplicationClassメインのスタートアップクラスのバイトコードオブジェクト
System.out出力ストリーム
跟进去 来到 org.springframework.boot.SpringApplicationBannerPrinter#print(org.springframework.core.env.Environment, java.lang.Class<?>, java.io.PrintStream)
getBanner
看下这个 getBanner(environment) 从配置环境中拿 Banner对象的方法
首先创建了一个 Banners 对象 org.springframework.boot.SpringApplicationBannerPrinter.Banners
然后调用 banners.addIfNotNull(getImageBanner(environment));
getImageBanner(environment)
看下怎么从图片中拿到Banner对象
也就是图片只支持这三种格式
resourceLoader.getResource("banner." + ext)
复制代码
最重要看这行代码
去类加载路径下找 banner.gif,banner.jpg,banner.png文件 如果存在 则添加到 banners 集合中然后 立马返回
如果都没找到,继续执行
getTextBanner(environment)
banners.addIfNotNull(getTextBanner(environment));
复制代码
org.springframework.boot.SpringApplicationBannerPrinter#getTextBanner
private Banner getTextBanner(Environment environment) {
String location = environment.getProperty(BANNER_LOCATION_PROPERTY, DEFAULT_BANNER_LOCATION);
Resource resource = this.resourceLoader.getResource(location);
if (resource.exists()) {
return new ResourceBanner(resource);
}
return null;
}
复制代码
static final String DEFAULT_BANNER_LOCATION = "banner.txt";
复制代码
去类加载路径下找 banner.txt 文件 找到添加到 banners 集合里 然后立马返回
修改banner的方式二 类加载路径下创建 banner.文件
banner文件分两种 一种图片 图片的加载顺序为 gif,jpg,png
一种txt
每种类型只能添加一个 同时最多打印两个banner 一个为图片类型的 一个为txt类型的
如 我在resource路径下放了两个banner文件,一个图片一个txt
生成banner的网站
关于佛祖的ascii艺术字,Spring Boot 佛祖 Banner-bootschool.net
然后resource路径下先进一个banner.txt 粘贴 再启动项目