When Spring Boot is launched, we may want to put our company logo or project logo on it. We can try these methods in this article. You can quickly make some Spring Boot project launch eggs to improve the recognition of the project. Or if you just want to add some fun to a boring life, this article will help you.
The knowledge points of this article are shown in the figure below:
Banner Effect Display
The default Spring Boot banner is displayed as follows:
Or something like this:
Or something like this:
Not only can you customize the content, you can also customize the color
Custom Banner
There are two ways to realize the custom banner, one is to rewrite the custom banner class to achieve, the other through TXT file to achieve.
1. Override the Banner class
First, we need a custom class to implement the Banner interface. 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(); }}Copy the code
Where the BANNER variable is the content of the custom BANNER, I put a hello, and then set the BANNER class to the custom class when Spring Boot is started, the implementation code is as follows:
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(DemoApplication.class);
// Set a custom Banner
springApplication.setBanner(new MyBanner());
// Start Spring Boot
springApplication.run(args);
}
Copy the code
The final result is shown below:
2. Use the TXT file
We can in the Spring the Boot engineering/SRC/main/resources directory to create a banner. TXT file, and then went in ASCII character painting copy, can replace the default banner, as shown in the figure below:
The reason why you can use the banner. TXT file to customize the banner is that the Spring Boot framework will find the banner information in the following order during startup:
- Find the banner. GIF or banner. JPG or banner.
- If none of these are present, go to Classpath and find banner.txt.
- The default SpringBootBanner is used if none is found.
Knowledge is in SpringApplicationBannerPrinter source code, the above 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"};
// Ignore non-core source code
private Banner getBanner(Environment environment) {
SpringApplicationBannerPrinter.Banners banners = new SpringApplicationBannerPrinter.Banners();
// Get the image form banner
banners.addIfNotNull(this.getImageBanner(environment));
// Get the text 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 newImageBanner(resource); }}return null; }}}Copy the code
So we can use the banner. TXT to customize the banner information, of course, you can also use the image to customize the banner.
Tip: We can use banner.gif to create dynamic banners. Try it out.
This method is simple to implement and non-intrusive, so it is recommended.
Banner style control
We can also modify the banner presentation and other properties, such as font style, bold, italic, etc. Spring Boot provides three enumerated classes to set these styles.
- AnsiColor: The foreground color used to set the character;
- AnsiBackground: Used to set the background color of a character.
- AnsiStyle: Used to control bold, italic, underlining, etc.
For example, we can use AnsiColor to set the color, with the following information in banner.txt:
${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-'Copy the code
The final display effect is as follows:
Banner uses output variables
In banner.txt we can also print some global variables, such as:
- ${application.version} : Used to get the version number in the manifest.mf file;
- {application.version} Version information;
- ${spring-boot.version} : spring boot version number;
- {spring-boot.version} Indicates the version information.
The following is an example:
/ ¯ ¯ ¯ ¯ \ o - | [] [] | o | - _ - _ | / ¯ ¯ ¯ ¯ ¯ ¯ ¯ ¯ ¯ ¯ \ | | | « » | | | | | | | | | (o) | | (o) | | | | | __ | | __ | | __ | | __ | Spring Boot version: ${spring-boot.version}Copy the code
The Banner was generated online
Address of the online generated banner:
- www.bootschool.net/ascii
- www.network-science.de/ascii/
- Patorjk.com/software/ta…
- www.degraeve.com/img2txt.php
The first is recommended, using thumbnails as follows:
Hide the Banner
If we need to hide our banner information, we can do so in one of three ways.
1. Close the Banner with code
We can set the hidden banner before Spring Boot starts (RUN) as follows:
public class DemoApplication {
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(DemoApplication.class);
/ / hide the banner
springApplication.setBannerMode(Banner.Mode.OFF);
// Start Spring BootspringApplication.run(args); }}Copy the code
2. Hide the Banner in the configuration file
Hide the banner display in the Spring Boot configuration file application.properties by setting the following configuration:
spring.main.banner-mode=off
Copy the code
3. Hide the Banner in Idea
We can hide the banner in the debug configuration of Idea, as shown below:
Add: eggs
At the end of the article, a banner of colorful Buddha was 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}? Buddha bless no bugs and never change? ${AnsiColor.BRIGHT_YELLOW}?????????????????????????????????????? ${AnsiColor.BRIGHT_YELLOW}Copy the code
The realization effect is shown in the figure below:
conclusion
This article we talk about two methods of custom banner, custom banner class and the way of banner. TXT, which in the way of source code analysis explains why you can through banner. TXT custom banner information. We also talked about banner style control (color, font style, etc.) and global variable output methods, as well as the online generation of the banner map several addresses, and finally talked about 3 methods to hide the banner.
The last word
Carefully write each original, just to live up to your watch. Writing is a cool and helpful thing to do, and I hope to keep doing it. If you find it useful, please give me a like and it will encourage me to produce better articles.
Reference & acknowledgements
www.jianshu.com/p/c1f7617c9…
www.jianshu.com/p/9a2c20e37…
For more exciting content, please follow the wechat public account “Java Chinese Community”.