SpringApplication的
Banner printedBanner = printBanner(environment);
private Banner printBanner(ConfigurableEnvironment environment) {
//判断banner是否关闭
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);
}
// 默认console 打印在控制台
return bannerPrinter.print(environment, this.mainApplicationClass, System.out);
}
Mode是个枚举类包括三个值:
直接看print方法,因为是走的控制台,所以直接上面的最后一个return返回。
public Banner print(Environment environment, Class> sourceClass, PrintStream out) {
Banner banner = getBanner(environment);
banner.printBanner(environment, sourceClass, out);
return new PrintedBanner(banner, sourceClass);
}
private Banner getBanner(Environment environment) {
Banners banners = new Banners();
//获取图片Banner
banners.addIfNotNull(getImageBanner(environment));
//获取文本Banner
banners.addIfNotNull(getTextBanner(environment));
if (banners.hasAtLeastOneBanner()) {
return banners;
}
if (this.fallbackBanner != null) {
return this.fallbackBanner;
}
return DEFAULT_BANNER;
}
如果不做配置,默认情况下这个方法会return一个null返回。
private Banner getImageBanner(Environment environment) {
String location = environment.getProperty(BANNER_IMAGE_LOCATION_PROPERTY);
if (StringUtils.hasLength(location)) {
Resource resource = this.resourceLoader.getResource(location);
return (resource.exists() ? new ImageBanner(resource) : null);
}
// IMAGE_EXTENSION 这个常量值:{ "gif", "jpg", "png" }
for (String ext : IMAGE_EXTENSION) {
Resource resource = this.resourceLoader.getResource("banner." + ext);
if (resource.exists()) {
return new ImageBanner(resource);
}
}
return null;
}
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;
}
默认Banner的输出会调用SpringBootBanner中的这个方法。
@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();
}
循环按照line去读取定义的banner字符串。然后用StringBuilder拼接字符,调用printStream.println()进行打印。
默认打印值:
文件位置和文件内容(bug game over)
if (banners.hasAtLeastOneBanner()) {
return banners;
}
这次调用print的时候是调用SpringApplicationBannerPrinter中的
@Override
public void printBanner(Environment environment, Class> sourceClass,
PrintStream out) {
for (Banner banner : this.banners) {
banner.printBanner(environment, sourceClass, out);
}
}
这方法会调用ResourceBanner这个类中的printBanner,
控制台打印:
public void printBanner(Environment environment, Class> sourceClass,PrintStream out)
最后会调用到
private void printBanner(BufferedImage image, int margin, boolean invert,
PrintStream out) {
AnsiElement background = (invert ? AnsiBackground.BLACK : AnsiBackground.DEFAULT);
out.print(AnsiOutput.encode(AnsiColor.DEFAULT));
out.print(AnsiOutput.encode(background));
out.println();
out.println();
AnsiColor lastColor = AnsiColor.DEFAULT;
for (int y = 0; y < image.getHeight(); y++) {
for (int i = 0; i < margin; i++) {
out.print(" ");
}
for (int x = 0; x < image.getWidth(); x++) {
Color color = new Color(image.getRGB(x, y), false);
AnsiColor ansiColor = AnsiColors.getClosest(color);
if (ansiColor != lastColor) {
out.print(AnsiOutput.encode(ansiColor));
lastColor = ansiColor;
}
out.print(getAsciiPixel(color, invert));
}
out.println();
}
out.print(AnsiOutput.encode(AnsiColor.DEFAULT));
out.print(AnsiOutput.encode(AnsiBackground.DEFAULT));
out.println();
}
通过字符数组把解析图片得到的值进行替换。最后打印的Banner。
图片的原型
https://blog.csdn.net/u014740338/article/details/68502907