printBanner
The printBanner(environment) method is called in the constructor of 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 print mode
The Banner.Mode enumeration class sets the banner printing mode in three ways
/**
* Disable printing of the banner.
不打印
*/
OFF,
/**
* Print the banner to System.out.
打印到控制台
*/
CONSOLE,
/**
* Print the banner to the log file.
打印到日志文件
*/
LOG
复制代码
The printing mode can be set through the configuration file
#不打印
spring.main.banner-mode=off
#打印到控制台
spring.main.banner-mode=console
#打印到日志
spring.main.banner-mode=log
复制代码
Look back at the printBanner method
Then created the SpringApplicationBannerPrinter class to print the banner
SpringApplicationBannerPrinter bannerPrinter = new SpringApplicationBannerPrinter(resourceLoader, this.banner);
复制代码
resourceLoader is now the default resource loader
while this.banner = null
Look at this SpringApplicationBannerPrinter class is accessed for the first time, initializes the property values of static members and initializes DEFAULT_BANNER
The way to modify the banner one implements the Banner interface
So we can directly create a Banner class to implement the Banner interface to imitate SpringBootBanner
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();
}
}
复制代码
Modify the main method of the main startup class
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(AppTest.class);
springApplication.setBanner(new MyBanner());
springApplication.run( args);
}
复制代码
Effect:
Continue to analyze the printBanner method
org.springframework.boot.SpringApplication#printBanner
Call the bannerPrinter.print(environment, this.mainApplicationClass, System.out) method
Three parameters ConfigurableEnvironment configuration environment
mainApplicationClass The bytecode object of the main startup class
System.out output stream
跟进去 来到 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 粘贴 再启动项目