Back to: Spring Boot Tutorials
Spring Boot Application Properties
In this article, I am going to discuss Spring Boot Application Properties. Please read our previous article where we discussed Spring Boot Dependency Injection and Application Runner.
Spring Boot Application Properties
As we have learned by now, Spring Boot allows us to change the environment in which an application runs. There are several methods to change the environment of an application. Let us understand them through examples.
Let us use the sample project we created in the article “Developing First Spring Boot Web Application”. Now, our project’s Java source code looks like this:
Now verify that the code functions without errors, by running it and navigating onto the http://localhost:8080 website. You should see the output below:
Note that by default, the Tomcat application uses port 8080 to run the application. (this port number can be configured).
Modifying Application Properties via a Properties file
Step 1: Navigate to the src/main/resources/application.properties file. It should be empty, by default.
Step 2: Add the following lines to the file, to signify the port number:
The application reads this file before running.
Step 3: Save the application.properties file and then run the application. The change in port number must reflect in the terminal output:
Step 4: Verify this by logging onto http://localhost:9090. The webpage must be displayed on this page.
You may go to the old webpage (https://localhost:8080) to check. A 404 (resource missing) error should occur on this website.
Modifying Application Properties via YAML
YAML stands for Yet Another Markup Language. YAML is a markup language that can be used to store data in a human-readable format. It is mostly used to configure applications. In this case, we will also be using YAML to configure our Spring Boot application. Follow the steps below:
Step 1: Reset the application.properties file (remove 2 lines added in step 2 above). This is so that the two configuration files do not interfere with each other.
Step 2: Create a new file in the same directory as the application.properties file. This should be the src/main/resources folder. Name this file as application.yaml.
Step 3: The file shall be empty by default. Add the following lines to the file:
Note that the indentation should be exactly as shown in the above screenshot. This is because YAML is an indentation-based markup language.
Step 4: Run the application to verify that the change in port number is properly reflected:
You will get the following output.
How Many Properties are there in Spring Boot?
In Spring Boot, there are sixteen categories of properties:
- Core Properties
- Cache Properties
- Mail Properties
- JSON Properties
- Data Properties
- Transaction Properties
- Data Migration Properties
- Integration Properties
- Web Properties
- Templating Properties
- Server Properties
- Security Properties
- RSocket Properties
- Actuator Properties
- DevTools Properties
- Testing Properties
Some of the more commonly used properties are as follows:
Property | Parameters | Default Values | Description |
Debug | true/false | false | It enables/disables debug logs. |
spring.application.name | <name of the application> | – | It is used to set the application name. |
spring.config.name | <name of the config file> | application | It is used to set the config file name. |
spring.config.location | <location of the config file> | – | It is used to config the file name. |
server.port | <port on which the Tomcat server should run> | 8080 | Configures the HTTP server port |
logging.file | <name of the log file> | – | It is used to set the log file name. For example, data.log. |
In the next article, I am going to discuss Spring Boot Logging. Here, in this article, I try to explain Spring Boot Application Properties. I hope you enjoy this Spring Boot Application Properties article.