spring.banner.location=classpath:custom-banner.txt
Custom banner in spring boot
Upasana | October 29, 2019 | 4 min read | 9,257 views
When we start any spring boot app, it comes up with a default ascii banner on the console. In this tutorial we will learn how to create a custom banner for app startup.
Spring boot accepts ascii text, gif, jpg and png files for the custom startup banners.
Configuring Ascii text banner is really a two step process:
-
Generate Ascii text banner using online tool
-
Save it in
src/main/resources/banner.txt
file
Part 1. Creating custom ascii banner
We will use an online Spring Boot Ascii Banner generator to create custom banner which will be displayed during the application startup. There are multiple sites that allow us to do this:
You can use any of the above to generate text of your choice.
Part 2. Configuring Ascii banner in Spring Boot
Now we have created Ascii text banner, we need to save this text file with name banner.txt
under src/main/resources
so that Spring Boot can pick it up and display it at application startup.
Spring Boot by default will pick content from the banner.txt file under resources folder and display it on the startup.
If you want, you can customize this behaviour by specifying custom location for banner text, which can be configured in application.properties file using banner.location
property.
or if you are using yaml format:
spring.banner.location: classpath:custom-banner.txt
,--. ,--. ,--.
| | | | ,---. | | ,---. ,---. ,--,--,--. ,---.
| |.'.| | | .-. : | | | .--' | .-. | | | | .-. :
| ,'. | \ --. | | \ `--. ' '-' ' | | | | \ --.
'--' '--' `----' `--' `---' `---' `--`--`--' `----'
2019-05-24 00:03:12.334 INFO 7191 --- [ main] com.shunya.comments.ShunyaApp : The following profiles are active: dev,default
2019-05-24 00:03:13.185 INFO 7191 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
Specifying font and background color
We can also specify font and background color of the ascii banners using the below format:
For example, to choose white background and red font color, your ascii text banner will look like this:
${AnsiBackground.WHITE}${AnsiColor.RED}
,--. ,--. ,--.
| | | | ,---. | | ,---. ,---. ,--,--,--. ,---.
| |.'.| | | .-. : | | | .--' | .-. | | | | .-. :
| ,'. | \ --. | | \ `--. ' '-' ' | | | | \ --.
'--' '--' `----' `--' `---' `---' `--`--`--' `----'
Using banner variables
There is a provision for specifying additional banner variables inside your banner.txt file. You can use any of the following placeholders:
Variable | Description |
---|---|
|
The title of your application, as declared in MANIFEST.MF |
|
The Spring Boot version i.e. |
|
Formatted Spring Boot version i.e. |
|
The version number of your application, as declared in MANIFEST.MF, example 1.0 |
|
The formatted version number of your application, as declared in MANIFEST.MF, example (v1.0) |
Example ascii banner.txt with these placeholders will look like the following:
${AnsiBackground.WHITE}${AnsiColor.RED}
,--. ,--. ,--.
| | | | ,---. | | ,---. ,---. ,--,--,--. ,---.
| |.'.| | | .-. : | | | .--' | .-. | | | | .-. :
| ,'. | \ --. | | \ `--. ' '-' ' | | | | \ --.
'--' '--' `----' `--' `---' `---' `--`--`--' `----'
Spring Boot ${spring-boot.version}
${application.title} - ${application.formatted-version}${AnsiBackground.DEFAULT}
We can always put ${AnsiBackground.DEFAULT}
at the end of the file to restore default background color on the console.
Part 3. Image banners
In addition to a text file, you can also add a banner.gif, banner.jpg, or banner.png image file to your classpath or set the spring.banner.image.location property. Images are converted into an ASCII art representation and printed above any text banner. Other option is to upload these images to online tool and convert them to ASCII text.
spring.banner.image.location: classpath:banner.gif
Part 4. Additional configuration
Banner mode: Console, Log and off
We can configure if the spring boot banner will be printed to System.out (console), print to log file (log), or disable printing at all (off)
banner-mode | Description |
---|---|
|
print banner to console (System.out) |
|
print banner to log file |
|
disable banner printing |
For example, the following configuration will print the banner to console.
spring.main.banner-mode: console
Optionally, you can also configure this behavior using Java code:
public static void main(String[] args) {
SpringApplication application = new SpringApplication(MyApplication.class);
application.setBannerMode(Banner.Mode.OFF);
application.run(args);
}
Generate banner programmatically
You can use SpringApplication.setBanner(…)
method to programmatically generate a banner in Spring Boot application. All you need to do is to provide your own implementation for org.springframework.boot.Banner
interface.
An example implementation will look like this:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(MyApplication.class);
app.setBanner((environment, sourceClass, out) -> {
out.println("Javacodemonk Welcome Banner");
});
app.run(args);
}
}
That’s all for now. For more information, you can refer to Spring Boot documentation:
Top articles in this category:
- Setting a Random Port in Spring Boot Application at startup
- Custom TTL for Spring data Redis Cache
- Redis rate limiter in Spring Boot
- Basic Auth Security in Spring Boot 2
- SendGrid emails in Spring Boot
- Testing web layer in Spring Boot using WebMvcTest
- Feign RequestInterceptor in Spring Boot