Home  >  Spring Boot

Spring Boot Custom Banner Example

By Arvind Rai, January 24, 2017
On this page we will provide spring boot custom banner example. On the application startup, spring boot prints a default banner. We can change default banner using banner.txt file in classpath. We can also change default banner programmatically using spring boot Banner interface. If we want to show image as banner then put the image file in classpath named as banner.jpg, banner.gif or banner.png . Banner text file and banner image file can also be configured in application property file using banner.location and banner.image.location respectively. Spring boot provides banner variables to print additional information with banner. If needed we can disable banner completely.
We can use custom banner as following.
Text Banner: For text banner just create a file named as banner.txt with desired text and keep it at the location src\main\resources.
Image Banner: For image banner just create a file named as banner.gif and keep it at the location src\main\resources. Other file extensions such as jpg, png can also be used. Console should support to display image.

In the application.properties we can configure following properties related to banner.
banner.charset: It configures banner encoding. Default is UTF-8
banner.location: It is banner file location. Default is classpath:banner.txt
banner.image.location: It configures banner image file location. Default is classpath:banner.gif. File can also be jpg, png.
banner.image.width: It configures width of the banner image in char. Default is 76.
banner.image.height: It configures height of the banner image in char. Default is based on image height.
banner.image.margin: It is left hand image margin in char. Default is 2.
banner.image.invert: It configures if images should be inverted for dark terminal themes. Default is false.

Now here on this page we will provide example how to use custom banner step by step.

Software Used

We are using following software in our application.
1. Java 8
2. Maven 3.3.9
3. Spring Boot 1.4.3.RELEASE

Create Maven File

Find the maven file used in our example.
pom.xml
 <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.concretepage</groupId>
	<artifactId>spring-demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>
	<name>spring-demo</name>
	<description>Demo project for Spring Boot</description>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.4.3.RELEASE</version>
		<relativePath/>
	</parent>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
    	        <dependency>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-devtools</artifactId>
                        <optional>true</optional>
                </dependency> 
	</dependencies> 
	<build>
	   <plugins>
		<plugin>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-maven-plugin</artifactId>
		</plugin>
	   </plugins>
	</build>
</project> 

Create Custom Banner using banner.txt in Classpath

If we run our spring boot application then we get a default banner as follows.
 .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.4.3.RELEASE)

2017-01-24 20:59:38.097  INFO 4420 --- [  restartedMain] com.concretepage.MyApplication           : Starting MyApplication on Renu-PC with PID 4420 (F:\arvind\PROJECT\mars\spring-boot\spring-demo\target\classes started by Renu in F:\arvind\PROJECT\mars\spring-boot\spring-demo) 
To create custom banner we have to use a file named as banner.txt inside classpath of the spring boot application. We should care that file name must be banner.txt. Find our demo project structure.
Spring Boot Banner Example
In the project we have created banner.txt file as follows.
banner.txt
=========================
  CONCRETEPAGE
========================= 
Find the main class to initialize SpringApplication.
MyApplication.java
@SpringBootApplication
public class MyApplication {  
    public static void main(String[] args) {
	SpringApplication application = new SpringApplication(MyApplication.class);
	application.run(args);
    }       
}  
Now run it, we will see that default banner will not be displayed and the text banner which we have used in banner.txt will be displayed as follows.
=========================
  CONCRETEPAGE
=========================  

2017-01-23 18:51:38.095  INFO 432 --- [  restartedMain] com.concretepage.MyApplication           : Starting MyApplication on Renu-PC with PID 432 (F:\arvind\PROJECT\mars\spring-boot\spring-demo\target\classes started by Renu in F:\arvind\PROJECT\mars\spring-boot\spring-demo)
2017-01-23 18:51:38.101  INFO 432 --- [  restartedMain] com.concretepage.MyApplication           : No active profile set, falling back to default profiles: default 
Banner file can be configured in application property file. Spring boot provides banner.location property that is used to configure banner file location. Find the property file.
application.properties
banner.location = banner/my-banner.txt 
If we are configuring banner file using application property file then file name can be any custom name. Here in my example banner file name is my-banner.txt that is located in resources/banner folder. If we have placed banner.txt file in classpath as well as we have configured banner.location in property file then in this case banner file will be picked up from application property file i.e. configured by banner.location . The default banner charset is UTF-8 and to change charset, configure the property banner.charset in application property file. Spring boot can show image banner printed on start up. For this we have to put a file in classpath named as banner.jpg, banner.gif or banner.png . Images are converted into ASCII art representation. If we want to configure image path in property file then for that spring boot provides banner.image.location that is configured as follows.
application.properties
banner.image.location = banner/concretepage.jpg 
If we configure text and image banner both then spring boot will show both banner and image banner will be above text banner. To display additional information with banner on startup, spring boot provides banner variables as follows.

${application.version} : Picks the version number of our application from the property Implementation-Version configured in MANIFEST.MF file.
${application.formatted-version} : Picks the version number of our application configured in MANIFEST.MF file that will be (surrounded with brackets and prefixed with v).
${application.title} : Picks the application title from the property Implementation-Title configured in MANIFEST.MF file.
${spring-boot.version} : It displays the spring boot version that we are using such as 1.4.3.RELEASE .
${spring-boot.formatted-version} : It displays the Spring Boot version that we are using formatted for display (surrounded with brackets and prefixed with v) such as example (v1.4.3.RELEASE).
${AnsiColor.NAME} : It is used to make colorful banner where NAME is an ANSI escape code. Find the values for NAME from the link
${AnsiBackground.NAME} : It is used to change banner background color where NAME is an ANSI escape code. Find the values for NAME from the link.
${AnsiStyle.NAME} : It is used to change style of banner where NAME is an ANSI escape code. Find the values for NAME from the link.

We need to configure above properties in banner.txt file or in the banner file configured by banner.location in application property file. Now we will discuss a sample example. Find our banner.txt file using banner variables.
resources/banner.txt
=========================
      CONCRETEPAGE
=========================  
Application Version : ${application.version}
Application Formatted Version : ${application.formatted-version}
Application Title : ${application.title}
Spring Boot Version : ${spring-boot.version}
Spring Boot Formatted Version : ${spring-boot.formatted-version}
============================================== 
Find our MANIFEST.MF file used in our example.
resources/META-INF/MANIFEST.MF
Manifest-Version: 1.0
Implementation-Title: spring-demo
Implementation-Version: 0.0.1-SNAPSHOT
Implementation-Vendor-Id: com.concretepage
Build-Jdk: 1.8.0
Implementation-Vendor: Pivotal Software, Inc. 
Now we will test our application. If we run application from IDE using main class the application is not deployed completely and MANIFEST.MF file is not read, so banner will not pick its values. If we run application in exploded form using mvn spring-boot:run command from command prompt, again the MANIFEST.MF file will not be read. So to read MANIFEST.MF file we need to create JAR. Find steps to run the JAR.
1. Go to the root directory of the project using command prompt and run following command.
mvn clean package 
The above command will create an executable JAR file for example
spring-demo-0.0.1-SNAPSHOT.jar 
2. To run the JAR file use below command.
java -jar target/spring-demo-0.0.1-SNAPSHOT.jar 
Now we will get output as follows.
Spring Boot Banner Example
We will observe that the values from MANIFEST.MF file is being displayed as banner variables. To make banner colorful, spring boot provides AnsiColor.NAME and AnsiBackground.NAME where NAME is an ANSI escape code. NAME for AnsiColor.NAME can be found from the link and NAME for AnsiBackground.NAME can be found from the link. Now let us create a colorful banner. Find the banner.txt.
banner.txt
${AnsiColor.BRIGHT_BLUE} ${AnsiBackground.BRIGHT_RED} HELLOW WORLD! 

Create Custom Banner Programmatically

To create custom banner programmatically, we need to follow below steps.
1. Spring boot provides Banner interface that enables us to change banner programmatically. We will create a class that will implement Banner interface and override its method printBanner() to configure banner.
MyBanner.java
package com.concretepage;
import java.io.PrintStream;
import org.springframework.boot.Banner;
import org.springframework.core.env.Environment;
public class MyBanner implements Banner  {
	@Override
	public void printBanner(Environment arg0, Class<?> arg1, PrintStream arg2) {
		arg2.println("================================");
		arg2.println("----------Hello World!----------");
		arg2.println("================================");
	}
} 
2. Now we need to configure our banner class with SpringApplication. Find the application class.
MyApplication.java
@SpringBootApplication
public class MyApplication {  
    public static void main(String[] args) {
	SpringApplication application = new SpringApplication(MyApplication.class);
	application.setBanner(new MyBanner());
	application.run(args);
    }       
}  
3. The banner defined by printBanner() method will be displayed only when if we are not using banner.txt file in classpath and we have not configured banner.location property in application property file. Find the application startup output.
================================
----------Hello World!----------
================================
2017-01-24 22:53:21.386  INFO 5600 --- [  restartedMain] com.concretepage.MyApplication           : Starting MyApplication on Renu-PC with PID 5600 (F:\arvind\PROJECT\mars\spring-boot\spring-demo\target\classes started by Renu in F:\arvind\PROJECT\mars\spring-boot\spring-demo) 

Disable Banner

To disable banner completely we can do it using application property file as well as programmatically.
1. Using application property file we need to configure spring.main.banner-mode property with the value off as follows.
application.properties
spring.main.banner-mode = off 
2. To disable banner programmatically we need to call setBannerMode() method of the class SpringApplication while initializing it in main method and pass Banner.Mode.OFF value as follows.
MyApplication.java
@SpringBootApplication
public class MyApplication {  
    public static void main(String[] args) {
	SpringApplication application = new SpringApplication(MyApplication.class);
	application.setBannerMode(Banner.Mode.OFF);
	application.run(args);
    }       
} 
If we want to print banner on console then use Banner.Mode.CONSOLE and if we want to print banner on log then use Banner.Mode.LOG with setBannerMode() method.

Reference

Customizing the Banner

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS





Copyright ©2017 concretepage.com, all rights reserved |Privacy Policy | Contact Us