Configuration and Profiles Questions
application.properties vs application.yml?
Both set the same properties. .properties is flat key=value; .yml is hierarchical and less repetitive, and one YAML file can hold several profile documents separated by ---. If both exist, .properties wins for the same key. YAML is indentation-sensitive, so typos fail silently.
What are profiles and how do you activate one?
Profiles group configuration and beans per environment (dev, test, prod). Boot loads application.yml then application-{profile}.yml, which overrides it. Beans can be limited with @Profile("prod").
Example
Ways to activate:
java -jar shopease.jar --spring.profiles.active=prod
SPRING_PROFILES_ACTIVE=prod java -jar shopease.jarExample output
The following 1 profile is active: "prod"
@Value vs @ConfigurationProperties?
| @Value | @ConfigurationProperties |
|---|---|
one property at a time: @Value("${app.tax-rate}") | binds a whole prefix to a typed class/record |
| supports SpEL | no SpEL, but relaxed binding (tax-rate = TAX_RATE) |
| no validation | can be @Validated; IDE auto-complete with metadata |
@ConfigurationProperties(prefix = "shopease.payment")
@Validated
public record PaymentProperties(@NotBlank String apiKey, Duration timeout, int maxRetries) { }
// enable with @ConfigurationPropertiesScan on the main classWhat is the order of precedence for configuration sources?
Higher overrides lower (simplified):
- Command-line arguments (
--server.port=9000) SPRING_APPLICATION_JSON, Java system properties (-D)- OS environment variables (
SERVER_PORT=9000) - Profile-specific files outside the JAR, then inside the JAR
application.yml/.propertiesoutside the JAR, then inside the JAR@PropertySourceand default properties
Tip: This is why the same JAR runs in dev and prod: only env vars / arguments change.
How do you keep secrets like DB passwords out of the code?
Never commit them. Reference environment variables with placeholders, and inject the values from the platform: Docker/Kubernetes secrets, AWS Secrets Manager / GCP Secret Manager, HashiCorp Vault (Spring Cloud Vault), or a Config Server with encryption.
spring:
datasource:
url: ${DB_URL}
username: ${DB_USER}
password: ${DB_PASSWORD}How do you load a bean only when a property is set?
@Bean
@ConditionalOnProperty(name = "shopease.sms.enabled", havingValue = "true")
public SmsSender twilioSmsSender() { return new TwilioSmsSender(); }Useful for feature flags and switching implementations per environment without code changes.