Sub topic 12
Configuring the app with application.properties
This chapter assumes you can run a Spring Boot application, have a @RestController in place, and know basic Java and HTTP. You will change the app's behavior without touching Java code.
Why externalize configuration
Values such as a port number, a database URL, or a feature flag change between your laptop, a test server, and production. Hardcoding them means rebuilding the application for each environment. Spring Boot reads them from outside the compiled code instead. This is called externalized configuration.
The most common place for these values is a file named application.properties. It lives in src/main/resources, and Spring Initializr creates it (empty) for you.
Your first properties
A properties file holds one key=value pair per line. Boot loads it at startup and applies the keys it recognizes.
server.port=8081
spring.application.name=demoserver.portchanges the port of the embedded server from the default 8080.spring.application.namesets a logical name for the application.
Restart the app and call the endpoint on the new port.
curl http://localhost:8081/helloBoot auto-configuration reads these keys. You did not write any code to wire server.port into Tomcat.
Using YAML instead
Boot also reads application.yml. It expresses the same keys as nested structure.
server:
port: 8081
spring:
application:
name: demoBoth formats work. Pick one per project. If both files exist, Boot loads both, and application.properties wins when a key appears in each.
Reading your own properties
You can define your own keys and inject them into a bean with @Value.
greeting.message=Hello from configurationpackage com.example.demo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class GreetingController {
private final String message;
public GreetingController(@Value("${greeting.message:Hello default}") String message) {
this.message = message;
}
@GetMapping("/greeting")
public String greeting() {
return message;
}
}The text after the colon in ${greeting.message:Hello default} is a default. It is used when the key is missing, so the app still starts.
Where values come from
Boot merges several sources. When the same key appears more than once, the source higher in this list wins.
| Priority | Source | Typical use |
|---|---|---|
| Highest | Command-line arguments | One-off overrides |
| High | Environment variables | Containers and servers |
| Lower | `application.properties` in the working directory | Per-deployment files |
| Lowest | `application.properties` inside the JAR | Defaults |
This order lets you ship sensible defaults in the JAR and override them at deploy time.
Overriding at run time
You can override a packaged value without rebuilding.
java -jar target/demo-0.0.1-SNAPSHOT.jar --server.port=9090
SERVER_PORT=9091 java -jar target/demo-0.0.1-SNAPSHOT.jarEnvironment variables use uppercase names with underscores in place of dots.
Profiles
A profile is a named set of settings for one environment. A file named application-dev.properties applies only when the dev profile is active.
# application-dev.properties
server.port=8082java -jar target/demo-0.0.1-SNAPSHOT.jar --spring.profiles.active=devProfile-specific values override the same keys in the base file.
Common errors
- Port already in use. Another process holds the port. Change
server.portor stop the other process. - Changes are ignored. The file must be on the classpath, normally in
src/main/resources. Rebuild or restart after editing. - Unresolvable placeholder.
@Value("${missing.key}")without a default fails startup. Add a default or define the key. - Spaces and typos. Keys are case-sensitive in the file and a stray space in a value is kept.
Exercise
A JAR contains server.port=8081 in its packaged application.properties. You start it with the environment variable SERVER_PORT=9091 and the argument --server.port=9090. Which port does the server use, and why?
Show answer
Port 9090. Command-line arguments have higher priority than environment variables, and both override the packaged properties file.
Exercise
What does the expression ${greeting.message:Hello default} do when greeting.message is not defined?
Show answer
It resolves to the default text "Hello default", so the application starts normally instead of failing.