Spring Boot: Application Properties
Defining the application.properties
In each Spring Boot application, there are values that can be configured without having to recompile the Java code.
These are typically configured in a file called application.properties
which lives under:
src/main/resources/application.properties
In practice, it may feel like you have to recompile,
because you still have to run mvn:package
or spring-boot:run
which packages up this file into it’s proper location
in the jar
file or target
directory. However, no actual Java source get recompiled to make the changes that you make
in application.properties
take effect.
Examples of things that go in application.properties
The most common thing you’ll see in application.properties
are these two lines:
server.port: ${PORT:8081}
logging.level.org.springframework.web: DEBUG
What about application.yml
Note that some applications will use src/main/resources/application.yml
which serves the same function, but uses
a different syntax.
Should application.properties
go into git?
Typically we do put application.properties
into git, but we need to be careful.
- Some things that go into
application.properties
are meant to be kept secret such as database passwords - Other things are things that are meant to be configured as you run or debug (e.g. debug levels), or adjusted for development, QA and production
We’ll discuss more how to handle these cases as they arise. The most important one is handling secrets, which is discussed below.
Handling Secrets
Note that some instructions will tell you to put certain things in the application.properties
that should really NEVER go into git.
One way of handling this is to define an Environment Variable.
- When running on localhost, this is a Unix, MacOS, or Windows Environment Variable.
- When running on Heroku, this is a Config Variable.
Names of environment variables are, by convention, all upper case and use snake case (underscores).
The environment variable used
by Spring Boot to override application.properties
values is called SPRING_APPLICATION_JSON
. It shoudl be set equal to a JSON string that defines keys/values for each application property that needs to be override or supplement the values in the application.properties
file.
An example of this is illustrated in the repo: https://github.com/ucsb-cs56-pconrad/spring-boot-app-config
More on application.properties
Related topics:
- Spring Boot: —A Java web application framework
- Spring Boot: Actuator—Checking the endpoint mappings, health or other info about your Spring Boot app
- Spring Boot: Application Properties—Defining the application.properties
- Spring Boot: ControllerAdvice—A place to factor out common ExceptionHandler, ModelAttribute and InitBinder code across multiple controllers
- Spring Boot: CSV—Downloading and Uploading CSV files with Spring Boot
- Spring Boot: Database Migrations—When you need to make a change to your database schema for an app in progress
- Spring Boot: Heroku—Tips for running with Spring Boot applications on Heroku
- Spring Boot: Logging—How to write information to the log in Spring Boot
- Spring Boot: OAuth—How to implement OAuth for authentication in Spring Boot
- Spring Boot: POST and CSRF—If you get 403 forbidden messages when using POST
- Spring Boot: Postgres—Using Spring Boot with Postgres
- Spring Boot: RestTemplate—When you need to access other APIs from the backend of your Spring Boot Application
- Spring Boot: Secrets—Ways of keepings database credentials and OAuth client secrets out of Github
- Spring Boot: Security—Authentication, Authorization and other Security issues
- Spring Boot: Sessions—How to make the stateless HTTP protocol stateful
- Spring Boot: SQL—Working with SQL and Databases in Spring Boot
- Spring Boot: VS Code—Suggested VS Code extensions for working with Spring Boot