Spring Boot Externalized Configuration and Profiles 5 — Questions and Answers
Question 1: What is the effect of adding @Profile("!prod") to a @Bean method?
- The bean is created only in production
- The bean is created in all profiles except production (Correct answer)
- The bean is never created
- The bean is created only when no profiles are active
Correct answer: The bean is created in all profiles except production
The ! operator in @Profile expressions negates the condition, so !prod means the bean loads when the prod profile is NOT active.
Question 2: Which property overrides the default config file location in Spring Boot?
- spring.config.name
- spring.config.location (Correct answer)
- spring.application.config
- spring.profiles.location
Correct answer: spring.config.location
spring.config.location replaces the default search paths entirely with the specified locations.
Question 3: How does Spring Boot handle a @ConfigurationProperties class with a nested object field?
- Nested objects are not supported; use @Value instead
- The nested object must be annotated with @NestedConfigurationProperty
- Nested objects are bound automatically using dot-separated property names (Correct answer)
- Nested objects require a dedicated @Bean method
Correct answer: Nested objects are bound automatically using dot-separated property names
Spring Boot recursively binds nested POJO fields using dot notation, e.g., app.database.host maps to a nested object's host field.
Question 4: What Spring Boot feature allows overriding properties per-test without modifying application.properties?
- @MockBean
- @TestPropertySource (Correct answer)
- @PropertyOverride
- @ConfigOverride
Correct answer: @TestPropertySource
@TestPropertySource allows specifying properties or property files that override the application's default configuration during tests.
Question 5: Which bean validation annotation ensures a @ConfigurationProperties field is not null at startup?
- @Required
- @NotNull (Correct answer)
- @NonNull
- @Mandatory
Correct answer: @NotNull
@NotNull from javax.validation triggers a startup failure if the bound property is missing or null, when validation is enabled.
Question 6: What does the spring.profiles.include property do?
- It replaces spring.profiles.active
- It always adds the listed profiles on top of whatever active profiles are set (Correct answer)
- It includes profiles only in test environments
- It imports profile-specific YAML documents
Correct answer: It always adds the listed profiles on top of whatever active profiles are set
spring.profiles.include unconditionally activates additional profiles on top of those specified in spring.profiles.active.
Question 7: Which configuration approach is recommended for secrets like database passwords in a production Spring Boot app?
- Hardcode them in application.properties and commit to git
- Use environment variables or a secrets manager like Vault (Correct answer)
- Store them in YAML files with Base64 encoding
- Pass them as URL query parameters
Correct answer: Use environment variables or a secrets manager like Vault
Environment variables or a dedicated secrets manager keep credentials out of source control and allow secure injection at runtime.
What is the effect of adding @Profile("!prod") to a @Bean method?