🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Prerequisites
- JDK 17 or later
- Maven or Gradle
- IDE (IntelliJ IDEA, Eclipse, etc.)
Step 1: Set Up a Spring Boot Project
1.1 Create a New Spring Boot Project
Use Spring Initializr to create a new project with the following dependencies:
- Spring Web
Download and unzip the project, then open it in your IDE.
1.2 Configure application.properties
(Optional) Set up the application properties for your project. This file is located in the src/main/resources directory. You can customize the banner properties here if needed.
# src/main/resources/application.properties
# Server port
server.port=8080
# Banner mode (console, log, or off)
spring.main.banner-mode=console
Explanation:
spring.main.banner-mode: Specifies where the banner is displayed (console, log, or off).
Step 2: Create a Custom Banner
Spring Boot looks for a banner.txt file in the src/main/resources directory to display as the startup banner. You can create your own ASCII art banner or use an online tool to generate one.
2.1 Create banner.txt
Create a file named banner.txt in the src/main/resources directory and add your custom ASCII art banner. You can use an online ASCII art generator like TAAG to create your banner.
____ _ ____ _
| _ \ ___ __ _ __| | ___ | __ ) ___ ___ | |_
| | | |/ _ \ / _` |/ _` |/ _ \ | _ \ / _ \ / _ \| __|
| |_| | (_) | (_| | (_| | __/ | |_) | (_) | (_) | |_
|____/ \___/ \__,_|\__,_|\___| |____/ \___/ \___/ \__|
You can also add Spring Boot placeholders in your banner for dynamic content:
${application.version}: Displays the application version.${spring-boot.version}: Displays the Spring Boot version.
Example:
____ _ ____ _
| _ \ ___ __ _ __| | ___ | __ ) ___ ___ | |_
| | | |/ _ \ / _` |/ _` |/ _ \ | _ \ / _ \ / _ \| __|
| |_| | (_) | (_| | (_| | __/ | |_) | (_) | (_) | |_
|____/ \___/ \__,_|\__,_|\___| |____/ \___/ \___/ \__|
:: Spring Boot :: (v${spring-boot.version})
Step 3: Customize Banner Using Code
If you want to customize the banner using code, you can implement the Banner interface.
3.1 Create a Custom Banner Class
Create a new class named CustomBanner in the com.example.demo package (or any other package).
package com.example.demo;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.core.env.Environment;
import java.io.PrintStream;
public class CustomBanner implements Banner {
@Override
public void printBanner(Environment environment, Class<?> sourceClass, PrintStream out) {
out.println(" ____ _ ____ _ ");
out.println(" | _ \\ ___ __ _ __| | ___ | __ ) ___ ___ | |_ ");
out.println(" | | | |/ _ \\ / _` |/ _` |/ _ \\ | _ \\ / _ \\ / _ \\| __|");
out.println(" | |_| | (_) | (_| | (_| | __/ | |_) | (_) | (_) | |_ ");
out.println(" |____/ \\___/ \\__,_|\\__,_|\\___| |____/ \\___/ \\___/ \\__|");
out.println(" ");
out.println(" :: Custom Spring Boot Banner :: ");
}
}
3.2 Register the Custom Banner
Modify the main method in your DemoApplication class to register the custom banner.
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(DemoApplication.class);
app.setBanner(new CustomBanner());
app.run(args);
}
}
Step 4: Running and Testing the Application
4.1 Run the Application
Run the Spring Boot application using your IDE or the command line:
./mvnw spring-boot:run
4.2 Check the Custom Banner
When the application starts, you should see your custom banner displayed in the console or log, depending on your configuration.
Conclusion
In this tutorial, you have learned how to customize the startup banner in a Spring Boot application. We covered:
- Setting up a Spring Boot project.
- Creating a custom ASCII art banner in a
banner.txtfile. - Adding dynamic content to the banner using placeholders.
- Implementing a custom banner programmatically using the
Bannerinterface. - Registering and displaying the custom banner.
By following these steps, you can create and display custom banners in your Spring Boot applications to provide a unique and personalized startup experience.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment