Skip to main content

๐ŸŒฑ Spring Boot Interview Series – Q6 ๐Ÿ’ก: Profiles, Startup Magic & Config Tricks ๐Ÿš€๐Ÿ› ️๐Ÿ“œ

๐ŸŒฑ 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

}
๐Ÿ’ก Hidden Fact: If a bean is not annotated with @Profile, it loads in all profiles by default.
⚠️ Common Mistake: Forgetting to annotate environment-specific beans leads to conflicts when multiple beans of the same type exist.

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 and ApplicationRunner beans after context startup
  • Initializes embedded web server if needed
๐Ÿ’ก Unknown Fact: You can extend SpringApplication to customize startup behavior, e.g., register listeners or modify environment before context loads.
⚠️ Common Mistake: Trying to call SpringApplication.run() multiple times in the same JVM without proper configuration can lead to port conflicts or duplicate bean errors.


๐ŸŽจ How to Change the Spring Boot Startup Banner?

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);
๐Ÿ’ก Fun Fact: Many devs hide jokes or small app info in the banner — like Easter eggs for backend nerds ๐Ÿฃ.



๐Ÿ›  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);
๐Ÿ’ก Unknown Fact: You can create multiple application contexts in a single JVM — useful for advanced testing or modular microservices setups.
⚠️ Common Mistake: Confusing SpringApplicationBuilder with SpringApplication.run(). Builder gives more control, but most apps don’t need it.



๐Ÿ“œ How to Externalize Configurations in Spring Boot?

Externalized configs help you keep environment-specific values and sensitive info outside the jar:

๐Ÿ’ก Pro Tip: Spring Boot resolves properties in this priority: Command line → Env vars → application.properties/yml → default values.
⚠️ Common Mistake: Hardcoding DB credentials or API keys in properties — a big security risk!

๐ŸŽฌ 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

Popular posts from this blog

๐Ÿ” Is final Really Final in Java? The Truth May Surprise You ๐Ÿ˜ฒ

๐Ÿ’ฌ “When I was exploring what to do and what not to do in Java, one small keyword caught my eye — final . I thought it meant: locked, sealed, frozen — like my fridge when I forget to defrost it.”   But guess what? Java has its own meaning of final… and it’s not always what you expect! ๐Ÿ˜… Let’s break it down together — with code, questions, confusion, jokes, and everything in between. ๐ŸŽฏ The Confusing Case: You Said It's Final... Then It Changed?! ๐Ÿซ  final List<String> names = new ArrayList <>(); names.add( "Anand" ); names.add( "Rahul" ); System.out.println(names); // [Anand, Rahul] ๐Ÿคฏ Hold on... that’s final , right?! So how on earth is it still changing ? Time to dive deeper... ๐Ÿง  Why Is It Designed Like This? Here’s the key secret: In Java, final applies to the reference , not the object it points to . Let’s decode this like a spy mission ๐Ÿ•ต️‍♂️: Imagine This: final List<String> names = new ArrayList <>(); Be...

๐ŸŒŸ My Journey – From Zero to Senior Java Tech Lead ๐ŸŒŸ

 There’s one thing I truly believe… If I can become a Java developer, then anyone in the world can. ๐Ÿ’ฏ Sounds crazy? Let me take you back. ๐Ÿ•“ Back in 2015… I had zero coding knowledge . Not just that — I had no interest in coding either. But life has its own plans. In 2016, I got a chance to move to Bangalore and joined a Java course at a training center. That’s where it all started — Every day, every session made me feel like: "Ohhh! Even I can be a developer!" That course didn’t just teach Java — it gave me confidence . ๐Ÿงช Two Life-Changing Incidents 1️⃣ The Interview That Wasn't Planned Halfway through my course, I had to urgently travel to Chennai to donate blood to a family member. After that emotional rollercoaster, I found myself reflecting on my skills and the future. The next day, as I was preparing for my move to Bangalore to complete the remaining four months of my course, I randomly thought — "Let me test my skills... let me just see...

๐ŸŽข Java Loops: Fun, Fear, and ForEach() Fails

๐ŸŒ€ Oops, I Looped It Again! — The Ultimate Java Loop Guide You Won't Forget “I remember this question from one of my early interviews — I was just 2 years into Java and the interviewer asked, ‘Which loop do you prefer and why?’” At first, I thought, “Duh! for-each is cleaner.” But then he grilled me with cases where it fails. ๐Ÿ˜ต That led me to explore all loop types, their powers, and their pitfalls. Let’s deep-dive into every major Java loop with examples &  real-world guidance so you'll never forget again. ๐Ÿ” Loop Type #1: Classic For Loop — “The Old Reliable” ✅ When to Use: You need an index You want to iterate in reverse You want full control over loop mechanics ✅ Good Example: List<String> names = List.of("A", "B", "C"); for (int i = 0; i < names.size(); i++) { System.out.println(i + ": " + names.get(i)); } ๐Ÿ”ฅ Reverse + Removal Example: List<String> item...