Spring Boot在启动的时候会打印一个默认的SpringBoot的图案
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.0.1.RELEASE)
打印图案的行为由SpringApplicationBannerPrinter控制,要打印的图案通过getBanner()获取Banner并打印。
- 首先通过文件控制图案,检查是否存在banner文件,存在则构建ImageBanner和ResourceBanner去打印
- 其次通过自定义的Banner 实现类控制图案,注入fallbackBanner进行打印。
- 最后使用默认的SpringBootBanner打印默认图案
private Banner getBanner(Environment environment, Banner definedBanner) {
//1、文件控制图案
Banners banners = new Banners();
banners.addIfNotNull(getImageBanner(environment));
banners.addIfNotNull(getTextBanner(environment));
if (banners.hasAtLeastOneBanner()) {
return banners;
}
//2.自定义的Banner 实现类控制图案
if (this.fallbackBanner != null) {
return this.fallbackBanner;
}
//默认的SpringBootBanner
return DEFAULT_BANNER;
}
private static class Banners implements Banner {
private final List<Banner> banners = new ArrayList<Banner>();
public void addIfNotNull(Banner banner) {
if (banner != null) {
this.banners.add(banner);
}
}
public boolean hasAtLeastOneBanner() {
return !this.banners.isEmpty();
}
@Override
public void printBanner(Environment environment, Class<?> sourceClass,
PrintStream out) {
for (Banner banner : this.banners) {
banner.printBanner(environment, sourceClass, out);
}
}
}
文件控制图案
通过文件控制图案的方式里,会到配置文件 application.proeprties 里根据spring.banner.location
和spring.banner.image.location
找到文件和图片文档地址,如果没有就到classpath目录下找默认的banner.txt和banner.gif/banner.jpg/banner.png,默认文件也没有就返回null不采用文件内容控制,图片会根据颜色深度转成字符图案。
/* SpringApplicationBannerPrinter.java
*/
static final String BANNER_LOCATION_PROPERTY = "banner.location";
static final String DEFAULT_BANNER_LOCATION = "banner.txt";
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 BANNER_IMAGE_LOCATION_PROPERTY = "banner.image.location";
static final String[] IMAGE_EXTENSION = { "gif", "jpg", "png" };
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;
}
自定义Banner
定制化 ASCII 字符:http://network-science.de/ascii/
定制化 ASCII 图片:https://www.degraeve.com/img2txt.php
新建类实现Banner并实现printBanner方法
import java.io.PrintStream;
import org.springframework.boot.Banner;
import org.springframework.core.env.Environment;
public class HelloBanner implements Banner
{
@Override
public void printBanner(Environment environment, Class<?> sourceClass, PrintStream out) {
String banner = "## ## ######## ## ## ####### \r\n"
+ "## ## ## ## ## ## ## \r\n"
+ "## ## ## ## ## ## ## \r\n"
+ "######### ###### ## ## ## ## \r\n"
+ "## ## ## ## ## ## ## \r\n"
+ "## ## ## ## ## ## ## \r\n"
+ "## ## ######## ######## ######## ####### ";
out.println(banner);
}
}
SpringApplication设置banner
@SpringBootApplication
public class Application
{
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(Application.class);
springApplication.setBanner(new MyBanner());
springApplication.run(args);
}
}
关闭Banner
Banner有三种打印模式,默认是CONSOLE打印到控制台。
public interface Banner {
enum Mode {
OFF,
CONSOLE,
LOG
}
}
如果要关闭打印图案,在SpringBootApplication中后缀.bannerMode(Banner.Mode.OFF)
@SpringBootApplication
public class Application {
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).bannerMode(Banner.Mode.OFF)
.run(args);
}
}
也可以在配置文件中配置spring.main.banner-mode=off
关闭