0
0
Spring Bootframework~10 mins

Environment-based profiles in Spring Boot - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Environment-based profiles
Start Application
Check Active Profile
Load Profile-Specific Config
Initialize Beans with Profile
Run Application with Config
End
The application starts, checks which profile is active, loads the matching configuration, initializes beans accordingly, and runs with that setup.
Execution Sample
Spring Boot
spring.profiles.active=dev

@Configuration
@Profile("dev")
public class DevConfig {
  // dev beans
}
This code activates the 'dev' profile and loads beans only when 'dev' is active.
Execution Table
StepActionProfile ActiveConfig LoadedBeans InitializedResult
1Start Applicationnone yetnonenoneApplication starting
2Read spring.profiles.activedevapplication-dev.propertiesnoneDev profile active
3Initialize Beansdevapplication-dev.propertiesDevConfig beansBeans ready for dev
4Run Applicationdevapplication-dev.propertiesDevConfig beansApp running with dev config
5Enddevapplication-dev.propertiesDevConfig beansApplication running
💡 Application runs with the 'dev' profile active, loading dev-specific config and beans.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
activeProfilenulldevdevdev
configLoadednoneapplication-dev.propertiesapplication-dev.propertiesapplication-dev.properties
beansInitializednonenoneDevConfig beansDevConfig beans
Key Moments - 2 Insights
Why does the application load dev beans only when the 'dev' profile is active?
Because @Profile("dev") annotation tells Spring to initialize those beans only if the active profile matches 'dev', as shown in execution_table step 3.
What happens if no profile is set in spring.profiles.active?
Spring loads default config and beans without any profile-specific ones, so profile-specific beans are not initialized. This is implied by the 'none yet' profile in step 1.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the active profile at step 2?
Adev
Bprod
Cnone
Dtest
💡 Hint
Check the 'Profile Active' column at step 2 in the execution_table.
At which step are the dev beans initialized?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Beans Initialized' column in the execution_table.
If spring.profiles.active was set to 'prod', how would the configLoaded variable change?
AIt would be null
BIt would remain application-dev.properties
CIt would be application-prod.properties
DIt would be application-default.properties
💡 Hint
Refer to variable_tracker and how configLoaded matches the active profile.
Concept Snapshot
Environment-based profiles in Spring Boot:
- Use spring.profiles.active to set the active profile.
- Annotate beans/config with @Profile("name") to load conditionally.
- Spring loads profile-specific properties like application-dev.properties.
- Only beans for the active profile initialize.
- Helps separate configs for dev, test, prod environments.
Full Transcript
When a Spring Boot application starts, it checks which profile is active by reading the spring.profiles.active property. Based on this, it loads the matching profile-specific configuration file, such as application-dev.properties for the 'dev' profile. Then, Spring initializes only the beans annotated with @Profile matching the active profile. This way, the application runs with environment-specific settings and beans. If no profile is set, default configurations and beans load. This mechanism helps developers easily switch between environments like development, testing, and production by changing one setting.