Profile-based configuration helps you run your app with different settings for different situations, like development or production. It keeps your app flexible and easy to manage.
Profile-based configuration in Spring Boot
spring.profiles.active=profileName # Example: application-dev.properties server.port=8081 # Example: application-prod.properties server.port=80
spring.profiles.active in application.properties or as a command line argument to select the active profile.application-{profile}.properties files for each profile's settings.application-dev.properties settings.# application.properties
spring.profiles.active=dev# application-dev.properties server.port=8081 logging.level.root=DEBUG
# application-prod.properties server.port=80 logging.level.root=ERROR
# Command line
java -jar app.jar --spring.profiles.active=prodThis Spring Boot app reads a message from configuration based on the active profile and shows it at the home URL.
Create two files:
application-dev.propertieswithapp.message=Hello from Development!application-prod.propertieswithapp.message=Welcome to Production!
Set the active profile to dev or prod to see different messages.
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } @RestController class HelloController { @Value("${app.message}") private String message; @GetMapping("/") public String home() { return message; } }
You can activate multiple profiles by separating them with commas, like spring.profiles.active=dev,debug.
Profiles help keep sensitive info like passwords out of your main config.
Use @Profile annotation on beans to load them only for certain profiles.
Profiles let you switch app settings easily for different environments.
Create separate config files named application-{profile}.properties.
Activate profiles via properties file, command line, or environment variables.
Practice
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 AQuick Check:
Profiles = environment-specific settings [OK]
- Thinking profiles are for writing code
- Confusing profiles with UI design
- Believing profiles manage database manually
dev?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.Final Answer:
application-dev.properties -> Option AQuick Check:
Profile file = application-{profile}.properties [OK]
- Using underscore instead of dash
- Placing profile after .properties
- Starting filename with profile name
application.properties contains server.port=8080application-prod.properties contains server.port=9090And the active profile is set to
prod, what port will the Spring Boot app run on?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 BQuick Check:
Active profile properties override default [OK]
- Assuming default always applies
- Thinking both ports run simultaneously
- Ignoring profile activation
spring.profiles.active=dev in application.properties, but your app still uses default settings. What is the likely problem?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 CQuick Check:
Profile file must use dash, not underscore [OK]
- 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
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?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=testuses the correct Spring Boot command-line syntax.java -jar app.jar -Dspring.profiles.active=testincorrectly places the-DJVM argument after-jar; correct would bejava -Dspring.profiles.active=test -jar app.jar. Flags like-profile testand--profile=testare not supported by Spring Boot.Final Answer:
java -jar app.jar --spring.profiles.active=test -> Option DQuick Check:
Use --spring.profiles.active=profile on command line [OK]
- Using -profile or --profile flags incorrectly
- Confusing JVM -D options without proper placement
- Trying to set profile inside application.properties only
Ready to prove it?
Pass the test and this pattern moves unknown
