๐ฑ Spring Boot Interview Series – Q6 ๐ก: Profiles, Startup Magic & Config Tricks ๐๐ ️๐
๐ข How to Set an Active Profile at Runtime?
Spring Boot profiles allow you to create environment-specific beans or configs. You can activate profiles in multiple ways:
1️⃣ Using @Profile
Annotation
Annotate your beans or configuration classes to load only for specific profiles:
@Component
@Profile("dev")
public class DevDataSourceConfig {
// bean definitions for dev environment
}
@Profile
, it loads in all profiles by default.2️⃣ Activating Profile via Properties
spring.profiles.active=dev
3️⃣ Command Line / Runtime
java -jar app.jar --spring.profiles.active=dev
4️⃣ Programmatically
SpringApplication app = new SpringApplication(MyApp.class);
app.setAdditionalProfiles("dev");
app.run(args);
✨ What is SpringApplication.run() Doing Internally?
This is more than just a “start the app” method — it’s the Spring Boot engine:
- Creates the ApplicationContext depending on the app type (web or non-web)
- Loads all
@Configuration
and auto-configuration classes - Processes
@Bean
and@Component
annotated classes - Runs
CommandLineRunner
andApplicationRunner
beans after context startup - Initializes embedded web server if needed
SpringApplication
to customize startup behavior, e.g., register listeners or modify environment before context loads.SpringApplication.run()
multiple times in the same JVM without proper configuration can lead to port conflicts or duplicate bean errors.By default, Spring Boot prints a banner at startup. You can customize it:
1️⃣ Using banner.txt
Create src/main/resources/banner.txt
:
____ _ _
/ ___| ___ ___ __ _| |_(_) ___ _ __ ___
| | _ / _ \ / _ \ / _` | __| |/ _ \| '_ \/ __|
| |_| | (_) | (_) | (_| | |_| | (_) | | | \__ \
\____|\___/ \___/ \__,_|\__|_|\___/|_| |_|___/
2️⃣ Programmatically
SpringApplication app = new SpringApplication(MyApp.class);
app.setBanner((environment, sourceClass, out) -> out.println("๐ฅ Welcome to MyApp! ๐ฅ"));
app.run(args);
๐ What is the Purpose of SpringApplicationBuilder?
This is a fluent API alternative to SpringApplication
:
- Set active profiles
- Customize environment
- Register initializers or listeners
- Build modular applications or multiple contexts
new SpringApplicationBuilder(MyApp.class)
.profiles("dev")
.listeners(new MyListener())
.run(args);
๐ How to Externalize Configurations in Spring Boot?
Externalized configs help you keep environment-specific values and sensitive info outside the jar:
- application.properties / application.yml
- Environment variables:
DB_URL
,API_KEY
- Command-line args:
--server.port=9090
- Spring Cloud Config / Vault (centralized configs for multiple apps)
๐ฌ Wrapping Up
In this post, we covered:
- Setting active profiles ๐ฑ with
@Profile
annotation, runtime args, or programmatically - The inner workings of
SpringApplication.run()
✨ - Custom startup banners ๐จ and fun Easter eggs
- The power of
SpringApplicationBuilder
๐ for advanced modular setups - Externalizing configurations safely ๐ for environment-specific apps
๐ฅ Challenge: Play with different profiles, try a custom banner, and experiment with externalized configs. Notice how small changes drastically affect app behavior ๐
๐ฌ Which Spring Boot trick surprised you most? Comment below ๐ and share your discoveries!
๐ Hashtags: #SpringBoot #JavaDeveloper #InterviewPrep #JavaLearning #SpringTips #Microservices #CodingLife #TechBlog
Comments
Post a Comment