Sub topic 3
Keeping configuration outside the artifact
This chapter assumes you can build a Spring Boot JAR with Maven or Gradle and run it with java -jar. It covers how to change settings per environment without rebuilding the JAR.
Why configuration stays outside
Build once and deploy the same JAR to test, staging, and production. If a database URL or port is baked into the JAR, every environment needs its own build. You can no longer be sure that what you tested is what you shipped. Spring Boot reads settings from several places at start-up, so the JAR can carry safe defaults and the environment can override them.
Where values come from
Spring Boot merges many property sources. When two sources set the same key, the source with higher precedence wins. This list runs from lowest to highest, and it is shortened to the sources you will use most:
- Defaults inside the JAR (
application.properties). - Profile-specific files inside the JAR (
application-prod.properties). - Config files next to the JAR, or in a
configsubfolder of the working directory. - Operating system environment variables.
- Command-line arguments.
Placeholders for values that vary
The packaged file can name a variable instead of a value. Spring resolves ${...} at start-up.
# application.properties
server.port=8080
spring.datasource.url=${DB_URL}
spring.datasource.password=${DB_PASSWORD}If DB_URL is missing, start-up fails with an "Could not resolve placeholder" error. That failure is useful, because a misconfigured instance never starts serving traffic. To allow a fallback, write ${DB_URL:jdbc:h2:mem:dev}.
Environment variables and relaxed binding
Spring Boot maps environment variables to property names. Uppercase the name, replace dots and dashes with underscores, and set it in the shell:
export SERVER_PORT=9090
export SPRING_DATASOURCE_URL=jdbc:postgresql://db.internal:5432/orders
java -jar target/myapp-1.0.0.jarSERVER_PORT overrides server.port. This is called relaxed binding. It lets you configure the app without editing any file.
Profiles
A profile is a named set of configuration for one environment. Spring loads application.properties first. It then loads application-{profile}.properties for each active profile, and the profile file wins on conflicts.
# application-prod.properties
server.shutdown=graceful
logging.level.root=WARNActivate a profile at run time:
java -jar target/myapp-1.0.0.jar --spring.profiles.active=prodYou can also set the environment variable SPRING_PROFILES_ACTIVE=prod. This form is convenient in containers and systemd units, where editing the command line is awkward.
Command-line arguments
Arguments that start with -- have the highest precedence. They suit one-off overrides:
java -jar app.jar --server.port=9000 --spring.profiles.active=prodAvoid putting passwords here. Command-line arguments appear in process listings such as ps, where other users on the machine can read them. The next chapter covers secrets.
An external config file
Place an application.properties in a config directory next to the JAR. Spring reads it and it overrides the packaged file:
mkdir config
echo "server.port=9100" > config/application.properties
java -jar target/myapp-1.0.0.jarTo use another location, pass --spring.config.additional-location=file:/etc/myapp/. The additional location is read on top of the defaults, so you only list the keys that differ.
Common errors
| Symptom | Likely cause |
|---|---|
| "Could not resolve placeholder" at start-up | The environment variable is not set in the process that launches Java. |
| The profile file is ignored | The profile name is misspelled, or the file name does not match `application-{profile}`. |
| An override has no effect | A higher-precedence source sets the same key. |
| Variable works in your shell but not in a service | The service manager does not inherit your shell's environment. |
Exercise
The JAR contains server.port=8080. The environment sets SERVER_PORT=9090, and the launch command includes --server.port=7070. Which port does the application use, and why?
Show answer
Port 7070. Command-line arguments have higher precedence than environment variables, and both override the value packaged in the JAR.
Exercise
A teammate wants to use one JAR for staging and production, with different database URLs. Name two ways to do this without rebuilding.
Show answer
Use a placeholder such as ${DB_URL} and set a different environment variable in each environment. Alternatively, activate a profile (--spring.profiles.active=prod) that loads application-prod.properties, or supply an external config file next to the JAR.