Working with Application Properties
1. Using application.properties File
| Property |
Example |
Description |
server.port |
8080 |
HTTP listen port |
server.servlet.context-path |
/api |
Application base path |
spring.application.name |
orders-svc |
Used for logs, discovery |
logging.level.root |
INFO |
Root log level |
spring.datasource.url |
jdbc:postgresql://… |
JDBC URL |
Example: Equivalent YAML configuration
server:
port: 8080
servlet:
context-path: /api
spring:
application:
name: orders-svc
datasource:
url: jdbc:postgresql://localhost/orders
username: app
password: ${DB_PASS}
logging:
level:
root: INFO
com.example: DEBUG
| Aspect |
YAML |
Properties |
| Hierarchy |
Native indentation |
Dot notation |
| Lists |
- item |
key[0]=item |
| Multi-doc |
--- separator |
Not supported |
3. Understanding Property Precedence and Override Order
| # |
Source (highest first) |
| 1 |
DevTools spring-boot-devtools.properties in $HOME/.config/spring-boot |
| 2 |
@TestPropertySource / @SpringBootTest.properties |
| 3 |
Command-line args (--server.port=9090) |
| 4 |
SPRING_APPLICATION_JSON env / system property |
| 5 |
OS environment variables |
| 6 |
Java System properties (-D) |
| 7 |
External application-{profile}.yml |
| 8 |
Packaged application-{profile}.yml |
| 9 |
External application.yml |
| 10 |
Packaged application.yml |
| 11 |
@PropertySource on @Configuration |
| 12 |
Default properties (SpringApplication.setDefaultProperties) |
4. Using Profile-Specific Properties
| File |
Loaded When |
application.yml |
Always (base) |
application-dev.yml |
--spring.profiles.active=dev |
application-prod.yml |
Profile prod active |
application-test.yml |
Used in @ActiveProfiles("test") |
5. Binding Properties with @Value Annotation
| Syntax |
Behavior |
@Value("${app.name}") |
Required property |
@Value("${app.timeout:30}") |
Default if missing |
@Value("${app.urls}") on List<String> |
Comma-separated → List |
@Value("#{systemProperties['user.home']}") |
SpEL expression |
Example: Constructor injection with @Value
@Service
public class MailService {
private final String from;
public MailService(@Value("${mail.from:no-reply@example.com}") String from) {
this.from = from;
}
}
6. Creating Type-Safe Configuration (@ConfigurationProperties)
| Element |
Purpose |
@ConfigurationProperties("prefix") |
Bind hierarchical props to POJO |
@EnableConfigurationProperties |
Register on @Configuration |
@ConfigurationPropertiesScan |
Auto-discover annotated POJOs |
spring-boot-configuration-processor |
Generates IDE metadata JSON |
Example: ConfigurationProperties as record
@ConfigurationProperties("app.mail")
public record MailProps(String host, int port, String from, Duration timeout) {}
7. Using Environment Variables and System Properties
| Property |
Env Var (relaxed binding) |
spring.datasource.url |
SPRING_DATASOURCE_URL |
server.port |
SERVER_PORT |
app.api-key |
APP_API_KEY |
| System property |
-Dserver.port=9090 |
8. Validating Configuration Properties (@Validated)
Example: Validated configuration properties
@Validated
@ConfigurationProperties("app.mail")
public class MailProps {
@NotBlank private String host;
@Min(1) @Max(65535) private int port;
@Email private String from;
// getters/setters
}
| Annotation |
Constraint |
@NotBlank |
Non-null, non-empty after trim |
@Email |
RFC email format |
@Min/@Max |
Numeric range |
@Pattern(regexp=…) |
Regex match |
9. Using Relaxed Binding Rules
| Property Style |
Equivalent for app.firstName |
| Kebab |
app.first-name (recommended in YAML) |
| Camel |
app.firstName |
| Snake |
app.first_name |
| Upper env |
APP_FIRSTNAME / APP_FIRST_NAME |
10. Encrypting Sensitive Properties
| Tool |
Usage |
| Spring Cloud Config |
{cipher}… prefix decrypted by config server |
| Jasypt Spring Boot |
ENC(encryptedValue); pass jasypt.encryptor.password |
| HashiCorp Vault |
spring-cloud-starter-vault-config |
| K8s Secrets |
Mount as env or files |
Warning: Never commit secrets to git. Prefer env vars or secret stores.
11. Loading External Configuration Files
| Property |
Effect |
spring.config.location |
Replaces default search locations |
spring.config.additional-location |
Adds to default locations (preferred) |
spring.config.import=optional:file:./extra.yml |
Modern import mechanism |
spring.config.import=configtree:/etc/secrets/ |
K8s/Docker secret tree |
12. Using Random Values
| Placeholder |
Result |
${random.uuid} |
UUID string |
${random.int} |
Random int |
${random.int(10)} |
0–9 |
${random.int[100,200]} |
Range |
${random.long} |
Random long |
${random.value} |
Random string |