Banner
使用Spring Boot启动的jar包总是会显示一个Spring的图标。实际我们是可以自定义这个图标。Banner接口定义了打印banner的方法。
void printBanner(Environment environment, Class> sourceClass, PrintStream out);
SpringApplicationBannerPrinter
springboot用来创建banner对象的类,核心方法为getBanner即获取Banner对象。
private Banner getBanner(Environment environment) {
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;
}
getImageBanner和getTextBanner方法从相应的位置获取资源,然后使用响应的Banner打印相应资源。
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;
}
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);
}
for (String ext : IMAGE\_EXTENSION) {
Resource resource = this.resourceLoader.getResource("banner." + ext);
if (resource.exists()) {
return new ImageBanner(resource);
}
}
return null;
}
SpringBootBanner
SpringBootBanner是Banner的实现类,打印一个spring的图样。
ResourceBanner
读取资源,替换占位符,然后打印到相应io。
public void printBanner(Environment environment, Class> sourceClass,
PrintStream out) {
try {
String banner = StreamUtils.copyToString(this.resource.getInputStream(),
environment.getProperty("banner.charset", Charset.class,
StandardCharsets.UTF_8));
for (PropertyResolver resolver : getPropertyResolvers(environment,
sourceClass)) {
banner = resolver.resolvePlaceholders(banner);
}
out.println(banner);
}
catch (Exception ex) {
logger.warn("Banner not printable: " + this.resource + " (" + ex.getClass()
+ ": '" + ex.getMessage() + "')", ex);
}
}
ImageBanner
读取图片资源,然后打印到相应io。
手机扫一扫
移动阅读更方便
你可能感兴趣的文章