Do you think IIT Guwahati certified course can help you in your career?
No
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.
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:
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.
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:
Core Properties: These are the application properties concerned with the basic configuration details.
Cache Properties: Application properties dealing with cache memory management.
Mail Properties: The application properties dealing with the SMTP (Simple Mail Transfer Protocol).
JSON(JavaScript Object Notation) Properties: These properties are concerned with the serialization of JSON.
Data Properties: Application Properties that work on data manipulation and management.
Transaction Properties: These are the application properties concerned with the transaction execution.
Data Migration Properties: Application properties regarding the migration of data from one schema to another one.
Integration Properties: The application properties dealing with the Integration of data and packages.
Web Properties: These properties are concerned with the implementation of the web applications.
Templating Properties: Application properties which handles the httpsessions.
Server Properties: The application properties having the server details.
Security Properties: These are the application properties about the security and authentication details.
RSocket Properties: Application properties regarding the transport and socket transmission.
Actuator Properties: The application properties regarding the Cloud Foundry actuator endpoint security calls.
DevTools Properties: These properties are concerned with the details of the development tools.
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:
Property
Default Values
Description
Debug
false
It enables debug logs.
spring.application.name
It is used to set the application name.
spring.application.admin.enabled
false
It is used to enable admin features of the application.
spring.config.name
application
It is used to set config file name.
spring.config.location
It is used to config the file name.
server.port
8080
Configures 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.charset
UTF-8
Banner file encoding.
spring.banner.location
classpath:banner.txt
It 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.enabled
false
It is used to enable admin features for the application.
spring.config.location
It is used to config the file locations.
spring.config.name
application
It is used to set config the file name.
spring.mail.default-encoding
UTF-8
It 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-connection
false
It 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.port
8080
It 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.enabled
True
It is used to enable support of multi-part uploads.
spring.servlet.multipart.max-file-size
1MB
It 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-mode
role
It is used to set security authorize-mode to apply.
security.basic.enabled
true
It is used to enable basic authentication.
Spring.test.database.replace
any
Type of existing DataSource to replace.
Spring.test.mockmvc.print
default
MVC(Model-View-Controller) Print option
spring.freemaker.content-type
text/html
Content-Type value
server.server-header
Value to use for the server response header.
spring.security.filter.dispatcher-type
async, error, request
Security filter chain dispatcher types.
spring.security.filter.order
-100
Security 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.