Saturday, 21 July 2018

SpringBoot: Specify banner mode (disable, log, console)

Banner interface provides below enum constants to configure the banner.

         OFF: Disable printing of the banner.
         CONSOLE: Print the banner to System.out.
         LOG: Print the banner to the log file.
        
How to specify the banner mode?
There are two ways to specify the banner mode.
a.   Specifying the property 'spring.main.banner-mode' in ‘application.properties’ file.
b.   Programmatically setting the mode.
        
Specifying the property 'spring.main.banner-mode' in application.properties file.
Below statements are used to set the banner in different modes.
spring.main.banner-mode=off
spring.main.banner-mode=console
spring.main.banner-mode=log

programmatically setting the banner mode
Below statements disable the banner.
SpringApplication app = new SpringApplication(Application.class);
app.setBanner(new MyBanner());
app.setBannerMode(Banner.Mode.OFF);

Below statements set the banner mode to console.
SpringApplication app = new SpringApplication(Application.class);
app.setBanner(new MyBanner());
app.setBannerMode(Banner.Mode.CONSOLE);

Below statements set the banner mode to log.
SpringApplication app = new SpringApplication(Application.class);
app.setBanner(new MyBanner());
app.setBannerMode(Banner.Mode.LOG);

Find the below sample application.

MyBanner.java
package com.sample.myApp;

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 environment, Class<?> sourceClass, PrintStream out) {
  out.println("**********************************************");
  out.println("*\n**\n***");
  out.println("My Custom Banner");
  out.println("*\n**\n***");
  out.println("***********************************************");
 }

}

Application.java
package com.sample.myApp;

import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class Application {

 public static void main(String args[]) {
  SpringApplication app = new SpringApplication(Application.class);
  app.setBanner(new MyBanner());
  app.setBannerMode(Banner.Mode.OFF);

  ConfigurableApplicationContext appContext = app.run(args);

  appContext.close();
 }
}



Previous                                                 Next                                                 Home

No comments:

Post a Comment