Table of contents
1.
Introduction
2.
Spring Boot Application Properties
3.
Example of Application Properties in Spring Boot
3.1.
YAML Properties File
3.1.1.
Example of application.yml
4.
Spring Boot Property Categories
5.
Application Properties Table
6.
Frequently Asked Questions
6.1.
When to use application properties and application YAML?
6.2.
What are Spring Boot application properties?
6.3.
What is the heap size of Spring Boot application properties?
6.4.
Which is better YAML or properties file?
7.
Conclusion
Last Updated: Oct 31, 2024

Spring Boot Application Properties

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In software development, configuration management plays a crucial role in creating scalable and maintainable applications. Spring Boot, a popular framework for building Java applications, simplifies this process with its application properties feature. Application properties in Spring Boot allow developers to externalize configuration, enabling them to define settings for various environments, such as development, testing, and production, without modifying the source code.

Spring Boot Application Properties

In this article, we will learn about the Spring Boot Application Properties. We will go through the example of an application property. 

Spring Boot Application Properties

Spring Boot Framework comes has a built-in mechanism for application configuration using a file called the ‘application.properties’. It is located in the src/main/resources folder, as shown below in the following figure.

The Spring Boot framework provides various properties that can be configured in the ‘application.properties’ file. The properties have default values. We can set a property for the application. Spring Boot also enables us to define our own property when needed.

The ‘application.properties’ file allows us to run an application in another environment. Briefly, we can use the application.properties file to:

  • Configure the Spring Boot framework
  • define our application custom configuration properties

Example of Application Properties in Spring Boot

Let us have a look at how we can configure the application properties with an example.

#configuring application name 
spring.application.name = ApplicationExample
#configuring port 
server.port = 8081 


In the above file, we have configured the application name and port. Port 8081 represents that the application runs on port 8081.

YAML Properties File

The yml file provided by Spring Boot is used to configure the properties. Because the Snake YAML jar is on the classpath, the Yaml file works. We can use the application.yml file instead of the application.properties file, however the YML file must be present in the classpath.

Example of application.yml

spring: 
application: 
name: ApplicationExample
server: 
port: 8081 


The application name and port have been configured in the example above. The application operates on port 8081, as shown by the port number.

Spring Boot Property Categories

The following are the sixteen Spring Boot Property categories:

  1. Core Properties: These are the application properties concerned with the basic configuration details.
  2. Cache Properties: Application properties dealing with cache memory management.
  3. Mail Properties: The application properties dealing with the SMTP (Simple Mail Transfer Protocol).
  4. JSON(JavaScript Object Notation) Properties: These properties are concerned with the serialization of JSON.
  5. Data Properties: Application Properties that work on data manipulation and management.
  6. Transaction Properties:  These are the application properties concerned with the transaction execution.
  7. Data Migration Properties: Application properties regarding the migration of data from one schema to another one.
  8. Integration Properties: The application properties dealing with the Integration of data and packages.
  9. Web Properties:  These properties are concerned with the implementation of the web applications.
  10. Templating Properties: Application properties which handles the httpsessions.
  11. Server Properties: The application properties having the server details.
  12. Security Properties: These are the application properties about the security and authentication details.
  13. RSocket Properties: Application properties regarding the transport and socket transmission.
  14. Actuator Properties: The application properties regarding the Cloud Foundry actuator endpoint security calls.
  15. DevTools Properties: These properties are concerned with the details of the development tools.
  16. Testing Properties: The application properties dealing with testing and quality assurance.

Application Properties Table

A collection of common Spring Boot application properties can be found in the table below:

PropertyDefault ValuesDescription
DebugfalseIt enables debug logs.
spring.application.name It is used to set the application name.
spring.application.admin.enabledfalseIt is used to enable admin features of the application.
spring.config.nameapplicationIt is used to set config file name.
spring.config.location It is used to config the file name.
server.port8080Configures the HTTP(HyperText Transfer Protocol) server port
server.servlet.context-path It configures the context path of the application.
logging.file.path It configures the location of the log file.
spring.banner.charsetUTF-8Banner file encoding.
spring.banner.locationclasspath:banner.txtIt is used to set banner file location.
logging.file It is used to set log file name. For example, data.log.
spring.application.index It is used to set the application index.
spring.application.name It is used to set the application name.
spring.application.admin.enabledfalseIt is used to enable admin features for the application.
spring.config.location It is used to config the file locations.
spring.config.nameapplicationIt is used to set config the file name.
spring.mail.default-encodingUTF-8It is used to set default MimeMessage encoding.
spring.mail.host It is used to set SMTP(Simple Mail Transfer Protocol) server host. For example, smtp.example.com.
spring.mail.password It is used to set the login password of the SMTP server.
spring.mail.port It is used to set SMTP server port.
spring.mail.test-connectionfalseIt is used to test that the mail server is available on startup.
spring.mail.username It is used to set the login user of the SMTP server.
spring.main.sources It is used to set sources for the application.
server.address It is used to set the network address to which the server should bind.
server.connection-timeout It is used to set time in milliseconds that connectors will wait for another HTTP request before closing the connection.
server.context-path It is used to set the context path of the application.
server.port8080It is used to set HTTP port.
server.server-header It is used for the Server response header (no header is sent if empty)
server.servlet-path/It is used to set path of the main dispatcher servlet
server.ssl.enabled It is used to enable SSL(Secure Sockets Layer) support.
spring.http.multipart.enabledTrueIt is used to enable support of multi-part uploads.
spring.servlet.multipart.max-file-size1MBIt is used to set max file size.
spring.mvc.async.request-timeout It is used to set time in milliseconds.
spring.mvc.date-format It is used to set the date format. For example, dd/MM/yyyy.
spring.mvc.locale It is used to set locale for the application.
spring.social.facebook.app-id It is used to set the application's Facebook App ID.
spring.social.linkedin.app-id It is used to set application's LinkedIn App ID.
spring.social.twitter.app-id It is used to set the application's Twitter App ID.
security.basic.authorize-moderoleIt is used to set security authorize-mode to apply.
security.basic.enabledtrueIt is used to enable basic authentication.
Spring.test.database.replaceanyType of existing DataSource to replace.
Spring.test.mockmvc.printdefaultMVC(Model-View-Controller) Print option
spring.freemaker.content-typetext/htmlContent-Type value
server.server-header Value to use for the server response header.
spring.security.filter.dispatcher-typeasync, error, requestSecurity filter chain dispatcher types.
spring.security.filter.order-100Security filter chain order.
spring.security.oauth2.client.registration.* OAuth client registrations.
spring.security.oauth2.client.provider.* OAuth provider details.

 

Frequently Asked Questions

When to use application properties and application YAML?

Use application.properties for simple key-value configurations, as it’s straightforward and easy to read. Opt for application.yml when dealing with complex configurations or nested properties, as YAML supports hierarchical data structures more elegantly.

What are Spring Boot application properties?

Spring Boot application properties are configuration settings defined in the application.properties file, allowing developers to customize application behavior based on the environment. This flexibility enables seamless transitions between development, testing, and production configurations.

What is the heap size of Spring Boot application properties?

The heap size for a Spring Boot application can be configured through properties like -Xms and -Xmx, defining the initial and maximum heap memory. This configuration affects memory management and application performance during runtime.

Which is better YAML or properties file?

YAML is generally considered better for complex configurations due to its ability to represent hierarchical data structures clearly and concisely. However, properties files are simpler and may be preferable for straightforward key-value pairs, depending on project requirements.

Conclusion

In this article, we learnt about the Spring Boot Application Properties. Spring Boot application properties play a vital role in configuring and managing application settings across different environments. By using the application.properties or application.yml files, developers can easily customize their applications without altering the source code, promoting better maintainability and flexibility. Understanding how to leverage these properties effectively allows for smoother transitions between development, testing, and production environments, ultimately leading to more robust applications.

Recommended Readings:

You can also consider our Spring Boot Course to give your career an edge over others. 

Live masterclass