目录
1. 隐藏Banner
2. 修改SpringBoot默认Banner
3. 原理
(1)从入口类run到SpringApplication#run
(2)跳到SpringApplication#pringBanner
(3)跳到SpringApplicationBannerPrinter#print
(4)这里getBanner便是获取Banner对象,可以看到默认类路径下文件banner.txt
(5)接着到ResourceBanner#printBanner
1. 隐藏Banner
在入口类设置
@SpringBootApplication
@MapperScan("com.winrh.mapper")
@ServletComponentScan("com.winrh.filter")
public class DemoApplication {
public static void main(String[] args) {
/**
* 隐藏banner启动方式
*/
SpringApplication springApplication = new SpringApplication(DemoApplication.class);
//设置banner的模式为隐藏
springApplication.setBannerMode(Banner.Mode.CONSOLE);
//启动springboot应用程序
springApplication.run(args);
}
}
2. 修改SpringBoot默认Banner
不改入口类,直接在resources包创建banner.txt
██ ██ ██ ████ ██ ███████ ██ ██
░██ ░██░██░██░██ ░██░██░░░░██ ░██ ░██
░██ █ ░██░██░██░░██ ░██░██ ░██ ░██ ░██
░██ ███ ░██░██░██ ░░██ ░██░███████ ░██████████
░██ ██░██░██░██░██ ░░██░██░██░░░██ ░██░░░░░░██
░████ ░░████░██░██ ░░████░██ ░░██ ░██ ░██
░██░ ░░░██░██░██ ░░███░██ ░░██░██ ░██
░░ ░░ ░░ ░░ ░░░ ░░ ░░ ░░ ░░
在线生成SpringBoot的Banner画,可以了解一下
3. 原理
为什么加个banner.txt就可以替换呢? 看一下源码
(1)从入口类run到SpringApplication#run
public ConfigurableApplicationContext run(String... args) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
ConfigurableApplicationContext context = null;
Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList();
this.configureHeadlessProperty();
SpringApplicationRunListeners listeners = this.getRunListeners(args);
listeners.starting();
Collection exceptionReporters;
try {
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
ConfigurableEnvironment environment = this.prepareEnvironment(listeners, applicationArguments);
this.configureIgnoreBeanInfo(environment);
Banner printedBanner = this.printBanner(environment);
context = this.createApplicationContext();
exceptionReporters = this.getSpringFactoriesInstances(SpringBootExceptionReporter.class, new Class[]{ConfigurableApplicationContext.class}, context);
this.prepareContext(context, environment, listeners, applicationArguments, printedBanner);
this.refreshContext(context);
this.afterRefresh(context, applicationArguments);
stopWatch.stop();
if (this.logStartupInfo) {
(new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), stopWatch);
}
listeners.started(context);
this.callRunners(context, applicationArguments);
} catch (Throwable var10) {
this.handleRunFailure(context, var10, exceptionReporters, listeners);
throw new IllegalStateException(var10);
}
try {
listeners.running(context);
return context;
} catch (Throwable var9) {
this.handleRunFailure(context, var9, exceptionReporters, (SpringApplicationRunListeners)null);
throw new IllegalStateException(var9);
}
}
看到了Banner
Banner printedBanner = this.printBanner(environment);
(2)跳到SpringApplication#pringBanner
private Banner printBanner(ConfigurableEnvironment environment) {
// 判断是不是隐藏了
if (this.bannerMode == Mode.OFF) {
return null;
} else {
ResourceLoader resourceLoader = this.resourceLoader != null ? this.resourceLoader : new DefaultResourceLoader((ClassLoader)null);
SpringApplicationBannerPrinter bannerPrinter = new SpringApplicationBannerPrinter((ResourceLoader)resourceLoader, this.banner);
return this.bannerMode == Mode.LOG ? bannerPrinter.print(environment, this.mainApplicationClass, logger) : bannerPrinter.print(environment, this.mainApplicationClass, System.out);
}
}
(3)跳到SpringApplicationBannerPrinter#print
Banner print(Environment environment, Class<?> sourceClass, PrintStream out) {
Banner banner = this.getBanner(environment);
banner.printBanner(environment, sourceClass, out);
return new SpringApplicationBannerPrinter.PrintedBanner(banner, sourceClass);
}
(4)这里getBanner便是获取Banner对象,可以看到默认类路径下文件banner.txt
private Banner getTextBanner(Environment environment) {
String location = environment.getProperty("spring.banner.location", "banner.txt");
Resource resource = this.resourceLoader.getResource(location);
return resource.exists() ? new ResourceBanner(resource) : null;
}
(5)接着到ResourceBanner#printBanner
public void printBanner(Environment environment, Class<?> sourceClass, PrintStream out) {
try {
String banner = StreamUtils.copyToString(this.resource.getInputStream(), (Charset)environment.getProperty("spring.banner.charset", Charset.class, StandardCharsets.UTF_8));
PropertyResolver resolver;
for(Iterator var5 = this.getPropertyResolvers(environment, sourceClass).iterator(); var5.hasNext(); banner = resolver.resolvePlaceholders(banner)) {
resolver = (PropertyResolver)var5.next();
}
out.println(banner);
} catch (Exception var7) {
logger.warn(LogMessage.format("Banner not printable: %s (%s: '%s')", this.resource, var7.getClass(), var7.getMessage()), var7);
}
}
其中String banner获取到banner的字符串,打印出来