在SpringBoot中有一个打印Banner的功能,只用在Classpath下添加字符文件,就可以在启动时候打印。但是如果当前的项目是Spring项目,该如何添加这个功能呢?
首先在pom.xml文件中添加SpringBoot的依赖
<properties>
<springframework.version>4.3.5.RELEASE</springframework.version>
<springboot.version>2.2.2.RELEASE</springboot.version>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
<jdk.source.veriosn>1.8</jdk.source.veriosn>
<jdk.target.veriosn>1.8</jdk.target.veriosn>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
<version>${springframework.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${springboot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
</dependencies>
SpringBoot的源码如下:
在org.springframework.boot.SpringApplication类中
/**
* Default banner location.
*/
public static final String BANNER_LOCATION_PROPERTY_VALUE = SpringApplicationBannerPrinter.DEFAULT_BANNER_LOCATION;
/**
* Banner location property key.
*/
public static final String BANNER_LOCATION_PROPERTY = SpringApplicationBannerPrinter.BANNER_LOCATION_PROPERTY;
public ConfigurableApplicationContext run(String... args) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
ConfigurableApplicationContext context = null;
Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
configureHeadlessProperty();
SpringApplicationRunListeners listeners = getRunListeners(args);
listeners.starting();
try {
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
configureIgnoreBeanInfo(environment);
// 打印Banner
Banner printedBanner = printBanner(environment);
context = createApplicationContext();
exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,
new Class[] { ConfigurableApplicationContext.class }, context);
prepareContext(context, environment, listeners, applicationArguments, printedBanner);
refreshContext(context);
afterRefresh(context, applicationArguments);
stopWatch.stop();
if (this.logStartupInfo) {
new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
}
listeners.started(context);
callRunners(context, applicationArguments);
}
catch (Throwable ex) {
handleRunFailure(context, ex, exceptionReporters, listeners);
throw new IllegalStateException(ex);
}
try {
listeners.running(context);
}
catch (Throwable ex) {
handleRunFailure(context, ex, exceptionReporters, null);
throw new IllegalStateException(ex);
}
return context;
}
private Banner printBanner(ConfigurableEnvironment environment) {
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);
}
return bannerPrinter.print(environment, this.mainApplicationClass, System.out);
}
但是很遗憾 用于打印的org.springframework.boot.SpringApplicationBannerPrinter类和默认的Banner类SpringBootBanner都定义是包内访问级别,因此拷贝这两个类到本地项目中任意一个包内。然后在同一个包内添加一个工具类:
import org.springframework.core.env.Environment;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.ResourceLoader;
public class TestBanner {
public static void printBanner() {
ResourceLoader resourceLoader = new DefaultResourceLoader();
Environment environment = new StandardEnvironment();
SpringApplicationBannerPrinter bannerPrinter = new SpringApplicationBannerPrinter(resourceLoader, null);
bannerPrinter.print(environment, null, System.out);
}
}
然后在需要打印banner的地方引用这个工具类即可。如果想自定义banner,只需要将文件存放到classpath目录下即可。
https://www.bootschool.net/ascii-art/search
可以在上面的网站查找banner,比如搜索 美女
,_ .--.
, , _)\/ ;--.
. ' . \_\-' | .' \
-= * =- (.-, / / |
' .\' ). ))/ .' _/\ /
\_ \_ /( / \ /(
/_\ .--' `-. // \
||\/ , '._// |
||/ /`(_ (_,;`-._/ /
\_.' ) /`\ .'
.' . | ;. /`
/ |\( `.(
| |/ | ` `
| | /
| |.'
__/' /
_ .' _.-`
_.` `.-;`/
/_.-'` / /
| /
jgs ( /
/_/