Spring Boot banner is a text message that is displayed at the start of a Spring Boot application.
This is how the default banner looks like.
This banner can be turned off or it is also possible to create a custom banner.
In this article, we'll learn how to create a custom banner and use it in Spring Boot applications.
Table of Content :
- Introduction
- Turning banner off
- Spring Boot custom banner
- Configuring Ascii banner in Spring Boot
- Using banner variables
- Generate banner programmatically
- Questions/Articles related to Spring Boot Custom Banner
- Summary
Turning banner off
The banner can be disabled using configuration settings or programmatically.
We can turn off the banner with the s
spring.main.banner-mode property. spring.main.banner-mode=off
or
SPRING_MAIN_BANNER-MODE=off
It is also possible to disable the banner with the SPRING_MAIN_BANNER-MODE environment variable.
new SpringApplicationBuilder(MyApplication.class)
.bannerMode(Banner.Mode.OFF)
.run(args)
var app = new SpringApplication(Application.class);
app.setBannerMode(Banner.Mode.OFF);
app.run(args);
Programatically, it is possible to turn off the banner with SpringApplicationBuilder or SpringApplication.
Configuring Ascii banner in Spring Boot
we need to create a file named banner.txt in the src/main/resources directory and paste the banner content into it.
if we want to choose any other location or another name for the banner, we need to set the spring.banner.location property in the application.properties file:
spring.banner.location=classpath:my-custom-banner.txt
spring.banner.image.location=classpath:my-custom-banner.png
if we are using images as banner then following properties can be configured.
spring.banner.image.location=classpath:banner.gif
spring.banner.image.width= 150
spring.banner.image.height= 120
spring.banner.image.margin= 10
spring.banner.image.invert= value
It is recommended that to use text format because the application startup time will drastically increase if some complex image structure is used.
or if you are using YAML Configuration
application.yml spring.banner.location: classpath:my-custom-banner.txt
Here is the output
,--. ,-----. ,--. ,---. ,--. ,---. ,---.
| | ,--,--. ,--. ,--. ,--,--. ' .--./ ,---. ,-| | ,---. ' .-' ,-' '-. ,--.,--. / .-' / .-' ,---.
,--. | | ' ,-. | \ `' / ' ,-. | | | | .-. | ' .-. | | .-. : `. `-. '-. .-' | || | | `-, | `-, ( .-'
| '-' / \ '-' | \ / \ '-' | ' '--'\ ' '-' ' \ `-' | \ --. .-' | | | ' '' ' | .-' | .-' .-' `)
`-----' `--`--' `--' `--`--' `-----' `---' `---' `----' `-----' `--' `----' `--' `--' `----'
2020-05-31 00:11:14.159 INFO 6743 --- [ main] com.bala.spring.myapp : The following profiles are active: dev,default
2020-05-31 00:11:15.175 INFO 6743 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
Using banner variables
We can customize the banner variables inside your banner.txt file. You can use following proerties
${application.title}
${spring-boot.version}
${spring-boot.formatted-version}
${application.version}
${application.formatted-version}
Generate banner programmatically
You can use SpringApplication.setBanner() method to programmatically generate a banner in the Spring Boot application.
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MySpringApp {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(MySpringApp.class);
app.setBanner((environment, sourceClass, out) -> {
out.println("JavaCodeStuff");
});
app.run(args);
}
}
Questions/Articles related to Spring Boot Custom Banner
Spring Boot Rest API exampleSpring Boot command line runner Example
Run Spring Boot App Without Web a Server
Spring Cloud AWS – S3 upload/download Examples
Difference Between @ComponentScan and @EnableAutoConfiguration in Spring Boot
That's it!! We have seen how to create Custom Banner in Spring Boot.
0 Comments
Post a Comment