0
0
Spring Bootframework~10 mins

@Profile for environment-specific beans in Spring Boot - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - @Profile for environment-specific beans
Start Application
Check Active Profiles
Match Bean @Profile?
NoSkip Bean
Yes
Register Bean in Context
Use Bean in Application
Application Runs with Correct Beans
The application starts, checks which profiles are active, registers only beans matching those profiles, and uses them during runtime.
Execution Sample
Spring Boot
@Configuration
@Profile("dev")
public class DevConfig {
  @Bean
  public DataSource devDataSource() {
    return new DevDataSource();
  }
}
Defines a bean that is only created when the 'dev' profile is active.
Execution Table
StepActive ProfilesBean Class@Profile AnnotationBean Registered?Reason
1devDevConfigdevYesActive profile matches @Profile
2devProdConfigprodNoActive profile 'dev' does not match 'prod'
3prodDevConfigdevNoActive profile 'prod' does not match 'dev'
4prodProdConfigprodYesActive profile matches @Profile
5noneDevConfigdevNoNo active profiles, bean skipped
6noneProdConfigprodNoNo active profiles, bean skipped
💡 Beans are only registered if their @Profile matches an active profile; otherwise, they are skipped.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
activeProfiles[]["dev"]["dev"]["prod"]["prod"][]
DevConfig Bean Registeredfalsetruetruefalsefalsefalse
ProdConfig Bean Registeredfalsefalsefalsefalsetruefalse
Key Moments - 2 Insights
Why is a bean with @Profile("dev") not created when the active profile is 'prod'?
Because the @Profile annotation tells Spring to only register the bean if the active profile matches exactly. See execution_table rows 3 and 4 where the mismatch causes skipping.
What happens if no profiles are active but beans have @Profile annotations?
Beans with @Profile annotations are not registered if no profiles are active, as shown in execution_table rows 5 and 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at step 2, is the ProdConfig bean registered when 'dev' profile is active?
AYes, it is registered
BOnly partially registered
CNo, it is not registered
DIt causes an error
💡 Hint
Check the 'Bean Registered?' column at step 2 in the execution_table.
According to variable_tracker, what is the value of 'activeProfiles' after step 4?
A["dev"]
B["prod"]
C[]
D["dev", "prod"]
💡 Hint
Look at the 'activeProfiles' row and the 'After Step 4' column in variable_tracker.
If the active profile is empty, what happens to beans annotated with @Profile("dev")?
AThey are skipped
BThey are registered anyway
CThey cause a startup error
DThey are registered but inactive
💡 Hint
Refer to execution_table rows 5 and 6 where no profiles are active.
Concept Snapshot
@Profile annotation limits bean creation to specific environments.
Activate profiles via application.properties or command line.
Only beans matching active profiles load into context.
No active profile means @Profile beans are skipped.
Use for dev, test, prod environment configs.
Full Transcript
When a Spring Boot application starts, it checks which profiles are active. Beans annotated with @Profile are only created if their profile matches one of the active profiles. For example, a bean with @Profile("dev") is created only if the 'dev' profile is active. If the active profile is 'prod', that bean is skipped. If no profiles are active, all @Profile beans are skipped. This helps run different beans in different environments like development or production. The execution table shows steps where beans are registered or skipped based on active profiles. The variable tracker shows how activeProfiles and bean registration flags change during startup.