Environment-based profiles help your application use different settings for different places, like your computer or a live server. This keeps things organized and safe.
Environment-based profiles in Spring Boot
spring.profiles.active=profileName
# In application-profileName.properties or application-profileName.yml
property.key=valueProfiles are named sets of configuration you can switch between.
You activate a profile by setting spring.profiles.active in your main config or environment.
dev and prod. The app uses port 8081 in dev and port 80 in prod.# application.properties spring.profiles.active=dev # application-dev.properties server.port=8081 # application-prod.properties server.port=80
test profile sets logging to debug level.# application.yml spring: profiles: active: test # application-test.yml logging: level: root: DEBUG
# Activate profile via command line
java -jar app.jar --spring.profiles.active=prodThis Spring Boot app has two beans. One runs only if the dev profile is active, the other only if prod is active. When you run the app with --spring.profiles.active=dev, it prints the dev message. With prod, it prints the prod message.
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Profile; import org.springframework.stereotype.Component; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } @Component @Profile("dev") class DevBean { public DevBean() { System.out.println("Dev profile active: Using development settings."); } } @Component @Profile("prod") class ProdBean { public ProdBean() { System.out.println("Prod profile active: Using production settings."); } }
You can have multiple profiles active at once by separating them with commas.
Use profiles to keep secrets and environment differences out of your main code.
Check which profile is active by printing or logging spring.profiles.active.
Profiles let your app use different settings for different environments.
Activate profiles in config files or command line.
Use @Profile to run code only for certain profiles.
Practice
What is the main purpose of using profiles in a Spring Boot application?
Solution
Step 1: Understand what profiles do
Profiles allow you to define different settings for different environments, such as development, testing, or production.Step 2: Match purpose with options
Only To separate configuration for different environments like development and production describes separating configuration for different environments, which is the main use of profiles.Final Answer:
To separate configuration for different environments like development and production -> Option AQuick Check:
Profiles = environment-specific settings [OK]
- Thinking profiles speed up startup
- Confusing profiles with threading
- Assuming profiles auto-generate schemas
Which of the following is the correct way to activate a profile named dev in application.properties?
Solution
Step 1: Recall the correct property key
The correct property to activate profiles isspring.profiles.active.Step 2: Compare options
Only spring.profiles.active=dev uses the exact correct key and value format.Final Answer:
spring.profiles.active=dev -> Option BQuick Check:
Activate profile with spring.profiles.active [OK]
- Using spring.active.profile instead
- Omitting 'spring' prefix
- Using singular 'profile' instead of 'profiles'
Given the following Spring Boot code snippet, what will be printed if the active profile is prod?
@Component
@Profile("dev")
public class DevService {
public DevService() {
System.out.println("DevService active");
}
}
@Component
@Profile("prod")
public class ProdService {
public ProdService() {
System.out.println("ProdService active");
}
}Solution
Step 1: Identify active profile
The active profile isprod, so only beans with@Profile("prod")will be created.Step 2: Determine which constructor runs
OnlyProdServiceconstructor runs, printing "ProdService active".Final Answer:
ProdService active -> Option AQuick Check:
@Profile("prod") runs only with prod profile [OK]
- Assuming both beans run regardless of profile
- Confusing dev and prod profiles
- Thinking no output prints if profile mismatches
Consider this Spring Boot configuration snippet in application.properties:
spring.profiles.active=dev,prod
What is the problem with this configuration?
Solution
Step 1: Understand multiple active profiles
Spring Boot allows multiple profiles active at once, but dev and prod usually have conflicting settings.Step 2: Identify the issue
Activating both dev and prod together can cause unexpected behavior or conflicts in configuration.Final Answer:
Activating both dev and prod profiles together can cause conflicts -> Option CQuick Check:
Dev and prod active together causes config conflicts [OK]
- Thinking comma separator is invalid
- Believing multiple profiles never cause issues
- Assuming spring.profiles.active can't be set in properties
You want to create a bean that only loads when test profile is active and another bean that loads for all profiles except test. Which annotation setup is correct?
Solution
Step 1: Use @Profile("test") for test-only bean
This ensures the first bean loads only when the test profile is active.Step 2: Use @Profile("!test") for all except test
The negation!testmeans the second bean loads when test is not active.Final Answer:
@Profile("test") on first bean, @Profile("!test") on second bean -> Option DQuick Check:
Use @Profile("!profile") to exclude a profile [OK]
- Not using @Profile on second bean causes it to always load
- Using @Profile("prod") limits second bean to prod only
- Assuming beans load automatically without profiles
Ready to prove it?
Pass the test and this pattern moves unknown
