What is the purpose of the spring.main.banner-mode property in Spring Boot?
Table of Contents
Introduction
In Spring Boot, the application startup process includes displaying a banner by default. This banner is typically used to show information about the Spring Boot application, such as the Spring logo, version number, or custom text. The banner is printed to the console when the application starts, providing a visual cue that the application is launching. The spring.main.banner-mode
property in Spring Boot allows you to control how this banner behaves, giving you flexibility in how much information is displayed at startup. In this guide, we will explore the purpose of the spring.main.banner-mode
property, its possible values, and how to customize it for your application.
What is the spring.main.banner-mode
Property?
The spring.main.banner-mode
property in Spring Boot controls the visibility and the mode in which the Spring Boot banner is displayed when the application starts. By default, Spring Boot displays a banner in the console with information about the application, but this behavior can be modified using this property. You can choose to show, hide, or even display the banner in a different format.
Possible Values for spring.main.banner-mode
The spring.main.banner-mode
property can be configured with the following values:
**console**
(Default)**off**
**log**
1. console
(Default Value)
By default, Spring Boot displays the banner in the console when the application starts. This is the standard mode, and it includes the Spring logo, version number, and other application-related details.
Example configuration:
This will show the default Spring Boot banner in the console when the application starts.
2. off
Setting spring.main.banner-mode=off
disables the banner entirely. If you don’t want to display the banner at all (for example, in production environments where you prefer a cleaner startup log), you can turn it off.
Example configuration:
With this setting, no banner will be printed during application startup.
3. log
When you set spring.main.banner-mode=log
, Spring Boot will log the banner as part of the logging output instead of displaying it directly in the console. This can be useful if you want to capture the banner in your application logs or if you prefer logging over console output.
Example configuration:
With this setting, the banner will not appear in the console but will be included in your application's log files.
Customizing the Banner with spring.main.banner-mode
While spring.main.banner-mode
controls how and where the banner is displayed, Spring Boot also provides a way to customize the content of the banner itself.
1. Using a Custom Banner File
You can create a custom ASCII banner or text banner and specify it in your project. By default, Spring Boot looks for a file named banner.txt
in the src/main/resources
directory. This file can contain any text you want to display when the application starts, such as a custom logo, application name, or version.
Example of a **banner.txt**
file:
2. Customizing Banner Using **banner.location**
You can specify a custom banner location by setting the spring.banner.location
property. For example, if you want to use a different file for your banner, you can specify its path.
Example:
This will load the banner from the custom-banner.txt
file located in the src/main/resources
directory.
3. Using a Banner in YAML Format
You can also create a YAML formatted banner. This is particularly useful when you want to use more structured content.
Example of a YAML-formatted banner (e.g., **banner.yml**
):
This YAML file can be used in conjunction with the spring.banner.location
property, pointing to a YAML file instead of the default banner.txt
.
4. Using a Banner via Java Configuration
In addition to external files, Spring Boot also allows you to programmatically customize the banner. You can do this by using the Banner
interface and implementing a custom banner logic inside the main
class of your Spring Boot application.
Example:
This code programmatically sets the banner mode to CONSOLE
and outputs a custom message in place of the default banner.
Practical Examples
Example 1: Disabling the Banner in Production
In production environments, you might prefer not to display the Spring Boot banner to avoid cluttering the logs or console output. You can disable the banner entirely using the spring.main.banner-mode
property.
In **application.properties**
:
This will ensure that no banner is shown when the application starts.
Example 2: Logging the Banner Instead of Showing It
In development environments, you might want to capture the banner in your application logs instead of showing it on the console. You can log the banner by setting the spring.main.banner-mode
property to log
.
In **application.properties**
:
This will ensure that the banner appears in your log files but not in the console output.
Example 3: Customizing the Banner with ASCII Art
You can add a custom ASCII art banner to be displayed when your Spring Boot application starts.
In **banner.txt**
:
This custom banner will appear on the console when the application starts.
Conclusion
The spring.main.banner-mode
property in Spring Boot provides flexibility in controlling how the Spring Boot banner is displayed during application startup. You can choose from three modes: console
(default), off
, and log
, depending on your requirements. Additionally, Spring Boot allows you to customize the banner's content and format, whether through text, YAML, or programmatically. Customizing the banner is a simple way to personalize your Spring Boot application or control what information is displayed when your application starts, making it cleaner or more informative based on your needs.