📖 Scenario: You are building a Spring Boot application that needs to behave differently in development and production environments. You want to use profile-based configuration to switch settings easily.
🎯 Goal: Create a Spring Boot application that uses application-dev.properties and application-prod.properties files to load different greeting messages based on the active profile.
📋 What You'll Learn
Create a base application.properties file
Create application-dev.properties with a greeting message
Create application-prod.properties with a different greeting message
Create a Spring Boot main class that reads the greeting message from properties
Use the @Value annotation to inject the greeting message
Use spring.profiles.active to switch between profiles
💡 Why This Matters
🌍 Real World
Profile-based configuration is used to manage different settings for development, testing, and production environments without changing code.
💼 Career
Understanding profile-based configuration is essential for backend developers working with Spring Boot to deploy applications safely and efficiently.
Progress0 / 4 steps
1
Create base application.properties and dev profile properties
Create a file named application.properties with no content. Then create application-dev.properties containing the line app.greeting=Hello from Development!.
Spring Boot
Hint
The application.properties file can be empty. The application-dev.properties file must contain the exact line app.greeting=Hello from Development!.
2
Create prod profile properties with a different greeting
Create application-prod.properties containing the line app.greeting=Hello from Production!.
Spring Boot
Hint
Make sure the application-prod.properties file contains the exact line app.greeting=Hello from Production!.
3
Create Spring Boot main class with greeting injection
Create a Spring Boot main class named ProfileDemoApplication in package com.example.demo. Use @Value("${app.greeting}") to inject the greeting into a String greeting field. Add a @RestController with a @GetMapping("/") method that returns the greeting string.
Spring Boot
Hint
Use @Value("${app.greeting}") to inject the greeting. Create a nested @RestController class with a @GetMapping("/") method returning the greeting.
4
Set active profile and run the application
In application.properties, add the line spring.profiles.active=dev to activate the dev profile. This will make the application use the dev greeting message when running.
Spring Boot
Hint
Add the exact line spring.profiles.active=dev to application.properties to activate the dev profile.
Practice
(1/5)
1. What is the main purpose of using profiles in Spring Boot configuration?
easy
A. To switch application settings easily for different environments
B. To write Java code faster
C. To create user interfaces
D. To manage database connections manually
Solution
Step 1: Understand what profiles do
Profiles allow you to have different settings for different environments like development, testing, and production.
Step 2: Identify the main use
Switching settings easily means you can run the same app with different configurations without changing code.
Final Answer:
To switch application settings easily for different environments -> Option A
Quick Check:
Profiles = environment-specific settings [OK]
Hint: Profiles = different settings for different environments [OK]
Common Mistakes:
Thinking profiles are for writing code
Confusing profiles with UI design
Believing profiles manage database manually
2. Which of the following is the correct way to name a Spring Boot properties file for a profile named dev?
easy
A. application-dev.properties
B. application.properties-dev
C. dev-application.properties
D. application_dev.properties
Solution
Step 1: Recall Spring Boot profile file naming
Spring Boot expects profile-specific files named as application-{profile}.properties.
Step 2: Match the correct pattern
For profile 'dev', the file should be application-dev.properties.
Hint: Profile files use dash between application and profile [OK]
Common Mistakes:
Using underscore instead of dash
Placing profile after .properties
Starting filename with profile name
3. Given these files: application.properties contains server.port=8080 application-prod.properties contains server.port=9090 And the active profile is set to prod, what port will the Spring Boot app run on?
medium
A. 8080
B. 9090
C. Both 8080 and 9090
D. No port will be set
Solution
Step 1: Understand profile override behavior
Properties in profile-specific files override those in the default application.properties when the profile is active.
Step 2: Apply active profile 'prod'
Since 'prod' is active, server.port=9090 from application-prod.properties overrides 8080.
Final Answer:
9090 -> Option B
Quick Check:
Active profile properties override default [OK]
Hint: Active profile settings override defaults [OK]
Common Mistakes:
Assuming default always applies
Thinking both ports run simultaneously
Ignoring profile activation
4. You wrote spring.profiles.active=dev in application.properties, but your app still uses default settings. What is the likely problem?
medium
A. The dev profile is not supported by Spring Boot
B. You need to restart the app after changing profiles
C. The profile-specific file is named application_dev.properties instead of application-dev.properties
D. Profiles cannot be set in application.properties
Solution
Step 1: Check profile file naming
Spring Boot requires profile files named with dash, not underscore.
Step 2: Identify mismatch
If the file is named application_dev.properties, Spring Boot won't recognize it for profile 'dev'.
Final Answer:
Incorrect profile file naming with underscore -> Option C
Quick Check:
Profile file must use dash, not underscore [OK]
Hint: Use dash, not underscore, in profile filenames [OK]
Common Mistakes:
Thinking profiles can't be set in application.properties
Forgetting to restart app (usually needed but not main issue here)
Believing 'dev' profile is unsupported
5. You want to activate the test profile without changing application.properties. Which of these is a valid way to do it when running your Spring Boot app from the command line?
hard
A. java -jar app.jar -profile test
B. java -jar app.jar -Dspring.profiles.active=test
C. java -jar app.jar --profile=test
D. java -jar app.jar --spring.profiles.active=test
Solution
Step 1: Recall command line profile activation syntax
Spring Boot uses --spring.profiles.active=profile to activate profiles via command line arguments.
Step 2: Check each option
java -jar app.jar --spring.profiles.active=test uses the correct Spring Boot command-line syntax. java -jar app.jar -Dspring.profiles.active=test incorrectly places the -D JVM argument after -jar; correct would be java -Dspring.profiles.active=test -jar app.jar. Flags like -profile test and --profile=test are not supported by Spring Boot.
Final Answer:
java -jar app.jar --spring.profiles.active=test -> Option D
Quick Check:
Use --spring.profiles.active=profile on command line [OK]
Hint: Use --spring.profiles.active=profile to activate profiles [OK]
Common Mistakes:
Using -profile or --profile flags incorrectly
Confusing JVM -D options without proper placement
Trying to set profile inside application.properties only
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.