When Spring Boot starts, we may want to put our company's logo or the project's logo on it. We can try these methods in this article, which will allow you to quickly make some Spring Boot project eggs at startup to improve The degree of recognition of the project, or pure broken in order to add some fun to the boring life, then these contents of this article can help you well.
The knowledge points of this article are as follows:
Banner effect display
The default banner of Spring Boot shows the effect as follows:
we can turn it into this:
Or something like this:
Or something like this:
Simply flying, not only can customize the content, but also customize the color , then let's take a look at how it is implemented.
Custom Banner
There are two ways to implement a custom banner. One is to rewrite the custom Banner class, and the other is to implement a txt file.
1. Rewrite the Banner class
First, you need to implement the Banner interface with a custom class. The implementation code is as follows:
import org.springframework.boot.Banner;
import org.springframework.core.env.Environment;
import java.io.PrintStream;
public class MyBanner implements Banner {
private static final String BANNER =
" ___ ___ .__ .__ \n" +
" / | \\ ____ | | | | ____ \n" +
"/ ~ \\_/ __ \\| | | | / _ \\ \n" +
"\\ Y /\\ ___/| |_| |_( <_> )\n" +
" \\___|_ / \\___ >____/____/\\____/ \n" +
" \\/ \\/ ";
@Override
public void printBanner(Environment environment, Class<?> sourceClass, PrintStream out) {
out.println(BANNER);
out.println();
}
}
The BANNER
variable is the content of the custom banner. I put one in here hello
, and then set the Banner class as a custom class when Spring Boot starts. The implementation code is as follows:
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(DemoApplication.class);
// 设置自定义 Banner
springApplication.setBanner(new MyBanner());
// 启动 Spring Boot
springApplication.run(args);
}
The final execution effect is shown in the figure below:
2. Through txt file
We can create a banner.txt file in the / src / main / resources directory of the Spring Boot project, and then copy the ASCII character painting into it to replace the default banner, as shown in the following figure:
The reason why you can use the banner.txt file to implement a custom banner is because the Spring Boot framework will search for banner information in the following order when it starts:
- First find the file banner.gif or banner.jpg or banner.png under Classpath, and use the one you find first;
- If you do n’t have any of the above, just look for banner.txt under Classpath;
- If none is found, the default Spring Boot Banner will be used.
The above knowledge points are learned in the SpringApplicationBannerPrinter source code, the core source code is as follows:
class SpringApplicationBannerPrinter {
static final String BANNER_LOCATION_PROPERTY = "spring.banner.location";
static final String BANNER_IMAGE_LOCATION_PROPERTY = "spring.banner.image.location";
static final String DEFAULT_BANNER_LOCATION = "banner.txt";
static final String[] IMAGE_EXTENSION = new String[]{"gif", "jpg", "png"};
// 忽略非核心源码
private Banner getBanner(Environment environment) {
SpringApplicationBannerPrinter.Banners banners = new SpringApplicationBannerPrinter.Banners();
// 获取图片形式 banner
banners.addIfNotNull(this.getImageBanner(environment));
// 获取文字形式 banner
banners.addIfNotNull(this.getTextBanner(environment));
if (banners.hasAtLeastOneBanner()) {
return banners;
} else {
return this.fallbackBanner != null ? this.fallbackBanner : DEFAULT_BANNER;
}
}
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;
}
private Banner getImageBanner(Environment environment) {
String location = environment.getProperty("spring.banner.image.location");
if (StringUtils.hasLength(location)) {
Resource resource = this.resourceLoader.getResource(location);
return resource.exists() ? new ImageBanner(resource) : null;
} else {
String[] var3 = IMAGE_EXTENSION;
int var4 = var3.length;
for(int var5 = 0; var5 < var4; ++var5) {
String ext = var3[var5];
Resource resource = this.resourceLoader.getResource("banner." + ext);
if (resource.exists()) {
return new ImageBanner(resource);
}
}
return null;
}
}
}
So we can use banner.txt to customize the banner information, of course, you can also use the way of pictures to customize the banner.
Tip: We can use banner.gif to achieve the effect of dynamic banner. Try it out.
This method is relatively simple to implement and is code-intrusive. This method is recommended.
Banner style control
The above mentioned the modification of the banner text. We can also modify the banner presentation and other attributes, such as font style, bold, italics, etc. Spring Boot provides three enumeration classes to set these styles. They are :
- AnsiColor: used to set the foreground color of characters;
- AnsiBackground: used to set the background color of the character.
- AnsiStyle: Used to control bold, italic, underline, etc.
For example, we can use AnsiColor to set the color. The information in banner.txt is as follows:
${AnsiColor.BRIGHT_RED} _ _ _ _
${AnsiColor.BRIGHT_RED} | || | ___ | | | | ___
${AnsiColor.BRIGHT_YELLOW} | __ | / -_) | | | | / _ \
${AnsiColor.BRIGHT_YELLOW} |_||_| \___| _|_|_ _|_|_ \___/
${AnsiColor.BRIGHT_RED}_|"""""|_|"""""|_|"""""|_|"""""|_|"""""|
${AnsiColor.BRIGHT_RED}"`-0-0-'"`-0-0-'"`-0-0-'"`-0-0-'"`-0-0-'
The final display effect is shown below:
Banner uses output variables
In banner.txt we can also output some global variables, for example:
- $ {application.version}: used to obtain the version number in the MANIFEST.MF file;
- $ {application.formatted-version}: formatted version information of $ {application.version};
- $ {spring-boot.version}: Spring Boot version number;
- $ {spring-boot.formatted-version}: Formatted version information of $ {spring-boot.version}.
Examples of use are as follows:
/¯¯¯¯\
o-|[][]|-o
|_--_|
/¯¯¯¯¯¯¯¯¯¯\
||| «» |||
||| |||
(o)| |(o)
| || |
|__||__|
|__||__|
Spring Boot 版本:${spring-boot.version}
Banner image online generation
Generate banner address online:
- https://www.bootschool.net/ascii
- http://www.network-science.de/ascii/
- http://patorjk.com/software/taag/
- http://www.degraeve.com/img2txt.php
It is recommended to use the first one, using thumbnails as follows:
Hide Banner
If we need to hide the banner information, we can use the following three methods .
1. Close the banner by code
We can set a hidden banner before Spring Boot starts (run), the implementation code is as follows:
public class DemoApplication {
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(DemoApplication.class);
// 隐藏 banner
springApplication.setBannerMode(Banner.Mode.OFF);
// 启动 Spring Boot
springApplication.run(args);
}
}
2. Hide Banner through the configuration file
The configuration file in Spring Boot application.properties
hides the banner display by setting the following configuration, the configuration is as follows:
spring.main.banner-mode=off
3. Hide Banner in Idea
We can hide the banner in Idea's debugging configuration, as shown in the following figure:
Attached: Easter eggs
At the end of the article, a banner of a colorful Buddha is attached:
${AnsiColor.BRIGHT_GREEN}$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
${AnsiColor.BRIGHT_YELLOW}$$ _.ooOoo._ $$
${AnsiColor.BRIGHT_RED}$$ o888888888o $$
${AnsiColor.BRIGHT_CYAN}$$ 88" . "88 $$
${AnsiColor.BRIGHT_MAGENTA}$$ (| ^_^ |) $$
${AnsiColor.BRIGHT_GREEN}$$ O\ = /O $$
${AnsiColor.BRIGHT_RED}$$ ____/`-----'\____ $$
${AnsiColor.BRIGHT_CYAN}$$ .' \\| |$$ `. $$
${AnsiColor.BRIGHT_MAGENTA}$$ / \\||| : |||$$ \ $$
${AnsiColor.BRIGHT_GREEN}$$ / _||||| -:- |||||- \ $$
${AnsiColor.BRIGHT_YELLOW}$$ | | \\\ - $$/ | | $$
${AnsiColor.BRIGHT_GREEN}$$ | \_| ''\-----/'' | | $$
${AnsiColor.BRIGHT_YELLOW}$$ \ .-\___ `-` ____/-. / $$
${AnsiColor.BRIGHT_CYAN}$$ ___`. .' /--.--\ `. . ___ $$
${AnsiColor.BRIGHT_RED}$$ ."" '< `.____\_<|>_/____.' >'"". $$
${AnsiColor.BRIGHT_GREEN}$$ | | : `- \`.;`.\ _ /``;.`/ - ` : | | $$
${AnsiColor.BRIGHT_YELLOW}$$ \ \ `-. \_ ___\ /___ _/ .-` / / $$
${AnsiColor.BRIGHT_CYAN}$$ ========`-.____`-.____\_____/____.-`____.-'======== $$
${AnsiColor.BRIGHT_MAGENTA}$$ `=---=' $$
${AnsiColor.BRIGHT_YELLOW}$$ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ $$
${AnsiColor.BRIGHT_GREEN}$$ 佛祖保佑 永无BUG 永不修改 $$
${AnsiColor.BRIGHT_YELLOW}$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
${AnsiColor.BRIGHT_YELLOW}
The effect is as follows:
to sum up
In this article, we have talked about two ways to customize banner, the way to customize banner class and banner.txt, which explains why we can pass banner.txt
custom banner information through source code analysis . We also talked about banner style control (color, font style, etc.) and global variable output methods, as well as several addresses generated by the banner map online, and finally talked about 3 ways to hide the banner.
Last words
Write each original carefully, just to live up to your watch. Writing is a cool thing that can help others, and I hope to stick to it. If you find it useful, please give me a thumbs up, he will encourage me to produce better articles.