Spring Boot Externalized Configuration and Profiles 3 — Questions and Answers
Question 1: What happens if you set spring.profiles.active=dev,test simultaneously?
- Only the first profile (dev) is applied
- An exception is thrown for conflicting profiles
- Both profiles are activated and their configs merged (Correct answer)
- Only the last profile (test) is applied
Correct answer: Both profiles are activated and their configs merged
Multiple profiles can be active at once; Spring Boot loads all matching profile-specific files and merges them.
Question 2: Which Spring Boot class provides programmatic access to which profiles are currently active?
- ApplicationContext
- Environment (Correct answer)
- ProfileResolver
- ConfigurableApplicationContext
Correct answer: Environment
The Environment interface exposes getActiveProfiles() and getDefaultProfiles() methods for programmatic profile inspection.
Question 3: What is the purpose of spring.profiles.default?
- It sets profiles used in production
- It specifies profiles activated when no active profiles are set (Correct answer)
- It overrides spring.profiles.active
- It defines the fallback profile for tests
Correct answer: It specifies profiles activated when no active profiles are set
spring.profiles.default specifies which profiles are active when no profiles are explicitly activated.
Question 4: Which YAML structure activates a properties block only for the 'prod' profile in a single application.yml?
- profiles: prod:
- spring.profiles: prod
- spring: config: activate: on-profile: prod (Correct answer)
- on-profile: prod:
Correct answer: spring: config: activate: on-profile: prod
In Spring Boot 2.4+, spring.config.activate.on-profile within a YAML document block restricts that block to a specific profile.
Question 5: How does @ConfigurationProperties differ from @Value for configuration binding?
- @ConfigurationProperties does not support YAML
- @ConfigurationProperties binds an entire prefix to a POJO; @Value injects a single property (Correct answer)
- @Value supports type-safe binding; @ConfigurationProperties does not
- There is no functional difference
Correct answer: @ConfigurationProperties binds an entire prefix to a POJO; @Value injects a single property
@ConfigurationProperties maps all properties under a prefix to fields in a class, whereas @Value injects individual property values.
Question 6: What annotation must accompany @ConfigurationProperties to register the class as a Spring bean directly?
- @Component (Correct answer)
- @Bean
- @EnableConfiguration
- @Inject
Correct answer: @Component
Adding @Component to a @ConfigurationProperties class registers it as a bean without needing a separate @Bean factory method.
Question 7: Which Spring Boot feature allows config values to be resolved from a Vault or Config Server at startup?
- spring.config.import (Correct answer)
- spring.datasource.url resolution
- spring.factories auto-configuration
- spring.cloud.bootstrap
Correct answer: spring.config.import
spring.config.import can target external config sources like spring-cloud-config-server:// or vault:// URIs.
What happens if you set spring.profiles.active=dev,test simultaneously?