Master java skills

Spring boot application properties file

Spring Boot comes with a built-in mechanism for application configuration using a file called application.properties. It is located under the src/main/resources folder, as shown below.

This file is like a properties file which stores key-value pair. Below is an example

Spring Boot provides various pre-defined properties that can be configured in application.properties file. The properties have default values unless configures specific properties.

Note -> Spring Boot also allows us to define our own property if required.

Application.properties file allows as to configure various properties. Some of the purposes are:

  • Configure the Spring Boot framework
  • Define custom configuration properties
  • Define environment specific properties

YAML file

There is another alternative to application.properties file and that is application.yaml file. The sytax used in YAML file is different. Either of the two files is used. Examples of a YAML file is given below

spring:  
    application:  
        name: spring boot sample application  
server:  
    port: 8081
spring:
    thymeleaf:
        enabled: true

Spring boot properties examples

Spring Boot pre-defined properties are divided into various categories. Some of the most common properties are listed in the below table.

PropertyDefault ValueDescription
spring.application.nameUsed to set the application name.
spring.config.nameapplicationIt is used to set config file name.
spring.application.admin.enabledfalseIt is used to enable admin features of the application.
server.port8080Used to set the port on which server will run
spring.config.locationIt is used to config the file name.
server.servlet.context-pathIt configures the context path of the application.
logging.file.pathIt configures the location of the log file.
logging.fileUsed to set log file name.
spring.banner.charsetUTF-8Banner file encoding.
spring.banner.locationclasspath:banner.txtIt is used to set banner file location.
spring.application.indexIt is used to set application index.
spring.application.nameIt is used to set the application name.
spring.application.admin.enabledfalseIt is used to enable admin features for the application.
spring.mail.default-encodingUTF-8It is used to set default MimeMessage encoding.
spring.mail.hostIt is used to set SMTP server host. For example, smtp.example.com.
spring.mail.passwordIt is used to set login password of the SMTP server.
spring.mail.portIt 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.usernameIt is used to set login user of the SMTP server.
spring.main.sourcesIt is used to set sources for the application.
server.addressIt is used to set network address to which the server should bind to.
server.connection-timeoutIt is used to set time in milliseconds that connectors will wait for another HTTP request before closing the connection.
server.context-pathIt is used to set context path of the application.
server.server-headerIt 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.enabledIt is used to enable SSL 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-timeoutIt is used to set time in milliseconds.
spring.mvc.date-formatIt is used to set date format. For example, dd/MM/yyyy.
spring.mvc.localeIt is used to set locale for the application.
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 Print option
spring.freemaker.content-typetext/htmlContent Type value
server.server-headerValue 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.