Spring Boot Externalized Configuration and Profiles 2 — Questions and Answers
Question 1: Which Spring Boot annotation enables a configuration class only when a specific profile is active?
- @ConditionalOnProfile
- @Profile (Correct answer)
- @ActiveProfile
- @EnableProfile
Correct answer: @Profile
@Profile on a @Configuration class or @Bean method restricts its registration to when the named profile is active.
Question 2: What is the property key used to activate profiles via application.properties?
- spring.application.profiles
- spring.profiles.include
- spring.profiles.active (Correct answer)
- spring.config.activate.on-profile
Correct answer: spring.profiles.active
spring.profiles.active sets the active profiles; it can hold a comma-separated list of profile names.
Question 3: When a property is defined in both application.properties and an environment variable, which wins by default?
- application.properties
- Environment variable (Correct answer)
- System property
- Command-line argument
Correct answer: Environment variable
Environment variables have higher precedence than application.properties in Spring Boot's property source order.
Question 4: How should the environment variable SPRING_DATASOURCE_URL be interpreted by Spring Boot?
- It is ignored; only .properties files are read
- It maps to spring.datasource.url (Correct answer)
- It maps to SPRING.datasource.url
- It requires explicit binding in @ConfigurationProperties
Correct answer: It maps to spring.datasource.url
Spring Boot's relaxed binding converts uppercase-with-underscores environment variables to dot-separated property names.
Question 5: Which file name pattern loads configuration only when the 'staging' profile is active?
- application-staging.properties (Correct answer)
- staging-application.properties
- application.staging.properties
- config-staging.properties
Correct answer: application-staging.properties
Profile-specific files follow the pattern application-{profile}.properties or application-{profile}.yml.
Question 6: What does spring.config.import do in Spring Boot 2.4+?
- It imports Java config classes into the Spring context
- It specifies additional config files or config trees to import (Correct answer)
- It replaces application.properties entirely
- It imports environment variables from a shell file
Correct answer: It specifies additional config files or config trees to import
spring.config.import allows importing additional configuration from files, directories, or config server locations.
Question 7: Which annotation on a test class overrides the active profiles for that test?
- @SpringBootTest(profiles=...)
- @ActiveProfiles (Correct answer)
- @TestProfile
- @OverrideProfile
Correct answer: @ActiveProfiles
@ActiveProfiles specifies which profiles should be active when loading the ApplicationContext for a test.
Which Spring Boot annotation enables a configuration class only when a specific profile is active?