How To Set Spring Boot Custom Banner

Spring Boot Custom Banner

Spring Boot applications display startup information in the console, usually starting with a banner:
  .   ____          _            __ _ _
 /\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.2.6.RELEASE)
    

How to Turn Banner Off

The banner can be turned off via configuration or Java code.
application.properties
Set the following property to turn off the banner:
spring.main.banner-mode=off
Main Application Class
@SpringBootApplication
public class HelloApplication implements CommandLineRunner {
    private static final Logger log = LoggerFactory.getLogger(HelloApplication.class);

    public static void main(String[] args) {
        var app = new SpringApplication(HelloApplication.class);
        app.setBannerMode(Banner.Mode.OFF);
        app.run(args);
    }

    @Autowired
    HelloService service;

    @Override
    public void run(String... args) throws Exception {
        log.info(service.hello());
    }
}
      

Changing the Banner

Provide ASCII banner content in a file and set its location via `spring.banner.location` in `application.properties`. You can include additional properties like `application.title` and `application.version` in the banner.

Code Details

my-banner.txt
ASCII banner using Spring Boot's `AnsiColor.RED` for coloring.
${AnsiColor.RED}
 _                    _                 
| |                  | |                
| |__    ___    ___  | |_  _ __    __ _ 
| '_ \  / _ \  / _ \ | __|| '_ \  / _` |
| |_) || (_) || (_) || |_ | | | || (_| |
|_.__/  \___/  \___/  \__||_| |_| \__, |
                                   __/ |
                                  |___/ 
Application Name: ${application.title}
Application Version: ${application.version}  
Spring Boot Version: ${spring-boot.version}
${AnsiColor.DEFAULT}
application.properties
#spring.main.banner-mode=off
spring.banner.location=classpath:my-banner.txt
application.title=Spring Boot Standalone App
application.version=1.0.0

Run The Application

mvn spring-boot:run
Console Output
 _                    _
| |                  | |
| |__    ___    ___  | |_  _ __    __ _
| '_ \  / _ \  / _ \ | __|| '_ \  / _` |
| |_) || (_) || (_) || |_ | | | || (_| |
|_.__/  \___/  \___/  \__||_| |_| \__, |
                                   __/ |
                                  |___/

Application Name: Spring Boot Standalone App
Application Version:
Spring Boot Version: 2.2.6.RELEASE
      

Git Source Code

  • git clone https://github.com/siddharthagit/spring-boot-references
  • cd spring-boot-standalone-app
  • mvn clean

No comments :

Post a Comment

Please leave your message queries or suggetions.

Note: Only a member of this blog may post a comment.