application.properties & @Value
Hard-coding ports, URLs, page sizes or feature flags into Java code means recompiling for every change. Spring Boot reads configuration from application.properties (or application.yml) in src/main/resources, plus environment variables and command-line arguments, and makes every value available to your beans.
properties or YAML
The same configuration in both formats:
spring.application.name=tasks
server.port=8081
tasks.page-size=20
tasks.welcome-message=Welcome to the task tracker
tasks.allowed-tags=work,home,urgentspring:
application:
name: tasks
server:
port: 8081
tasks:
page-size: 20
welcome-message: Welcome to the task tracker
allowed-tags: work,home,urgent.properties |
.yml |
|
|---|---|---|
| Syntax | flat key=value |
nested, indentation-based |
| Repetition of prefixes | yes | no |
| Lists | a,b,c or key[0]=a |
- a items or a,b,c |
| Pitfalls | few | tabs are illegal; indentation errors |
Use whichever your team prefers — but only one per project, to avoid confusion. If both exist, .properties wins for duplicate keys.
Injecting a single value with @Value
@RestController
class WelcomeController {
private final String message;
private final int pageSize;
WelcomeController(@Value("${tasks.welcome-message}") String message,
@Value("${tasks.page-size:10}") int pageSize) { // 10 = default
this.message = message;
this.pageSize = pageSize;
}
@GetMapping("/api/welcome")
Map<String, Object> welcome() {
return Map.of("message", message, "pageSize", pageSize);
}
}${...} is a placeholder resolved from the environment; text after a colon is the default used when the key is missing. Spring converts the string to the parameter type (int, boolean, Duration, List<String>...).
curl -s localhost:8081/api/welcome{"pageSize":20,"message":"Welcome to the task tracker"}
(Map.of has no defined iteration order, so the keys may appear in either order.)
How placeholders are resolved
Here is a simplified resolver supporting ${key} and ${key:default} — the same syntax Spring uses:
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class Main {
static final Pattern PLACEHOLDER = Pattern.compile("\\$\\{([^:}]+)(?::([^}]*))?}");
static String resolve(String text, Map<String, String> props) {
Matcher m = PLACEHOLDER.matcher(text);
StringBuilder out = new StringBuilder();
while (m.find()) {
String key = m.group(1);
String value = props.getOrDefault(key, m.group(2));
if (value == null) {
throw new IllegalArgumentException("Could not resolve placeholder '" + key + "' in value \"" + text + "\"");
}
m.appendReplacement(out, Matcher.quoteReplacement(value));
}
m.appendTail(out);
return out.toString();
}
public static void main(String[] args) {
Map<String, String> props = Map.of(
"server.port", "8081",
"tasks.welcome-message", "Welcome to the task tracker");
System.out.println(resolve("${tasks.welcome-message}", props));
System.out.println(resolve("http://localhost:${server.port}/api", props));
System.out.println(resolve("${tasks.page-size:10}", props));
try {
resolve("${tasks.api-key}", props);
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
}
}Welcome to the task tracker
http://localhost:8081/api
10
Could not resolve placeholder 'tasks.api-key' in value "${tasks.api-key}"
The last line mirrors Spring's real error message: a missing property without a default makes the application fail at startup, which is good — you find out immediately.
Placeholders also work inside property files:
app.base-url=https://tasks.example.com
app.docs-url=${app.base-url}/docsWhere values come from
Spring Boot merges many property sources. Higher entries override lower ones:
| Priority | Source | Example |
|---|---|---|
| 1 (highest) | command-line arguments | --server.port=9000 |
| 2 | SPRING_APPLICATION_JSON |
{"server":{"port":9000}} |
| 3 | OS environment variables | SERVER_PORT=9000 |
| 4 | application-{profile}.properties |
application-prod.properties |
| 5 | application.properties |
inside the JAR |
| 6 (lowest) | defaults in code | @Value("${x:default}") |
Environment variables use relaxed binding: uppercase, dots and dashes become underscores, so tasks.page-size can be set with TASKS_PAGESIZE or TASKS_PAGE_SIZE.
SERVER_PORT=9090 java -jar target/tasks-0.0.1-SNAPSHOT.jar --tasks.page-size=50 &
sleep 3
curl -s localhost:9090/api/welcome{"pageSize":50,"message":"Welcome to the task tracker"}
The JAR was not rebuilt — the port came from an environment variable and the page size from a command-line argument.
Built-in properties worth knowing
| Property | Purpose |
|---|---|
server.port |
HTTP port (default 8080; 0 = random free port) |
server.servlet.context-path |
URL prefix for the whole app, e.g. /tasks |
spring.application.name |
name used in logs and monitoring |
spring.jackson.default-property-inclusion |
e.g. non_null to omit null fields |
spring.threads.virtual.enabled |
use Java 21 virtual threads for request handling |
Tip:
@Valueis fine for one or two values. When a feature has several related settings, bind them to a typed class with@ConfigurationPropertiesinstead — that is the next lesson.
Test yourself
What does @Value("${tasks.page-size:10}") inject when tasks.page-size is not defined?
- null
- 0
- 10
- Startup fails
Which source wins: server.port=8081 in application.properties or --server.port=9000 on the command line?
- application.properties
- The command line
- Whichever is read first
- Neither; startup fails
Which environment variable sets tasks.page-size through relaxed binding?
tasks.page-sizeTASKS_PAGESIZETasks-Page-Sizetasks:page:size