What is the Use of Application Properties in Spring Boot

Posted By :Yasir Zuberi |30th May 2020

Application Properties in Spring Boot

 

Application Properties in Spring boot enable different configurations for different environments. Spring Boot's main advantage over the standard Spring framework is Lesser-Configuration. Spring Boot's convention over configuration is very well applied to application properties files. We, at Oodles, as an Artificial Intelligence Development Company, highlight the best practices while setting up application properties in the Spring boot environment.

 

Spring Boot provides a default "application.properties" file and it's autodetected without any extra configurations. We just need to place "application.properties" file inside the "src/main/resources" directory. In your properties file, there is no limit to add properties. You may add as many properties as per your requirement.

 

Default Properties File - application.properties Sample

 

server.port = 8090
spring.application.name = my-springboot-application
server.servlet.context-path =/my-springboot-application

 

Environment Specific Properties File

In multiple environments, we need a separate properties file for each environment. 

Let’s assume you have two environments, development and production. So you need two different application.properties like “application-development.properties” and “application-production.properties”

 

application-development.properties Sample Code

 

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql=false
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3306/testDB
spring.datasource.username=root
spring.datasource.password=root

 

application-production.properties Sample Code

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=none
spring.datasource.url=jdbc:mysql://production-server:3306/prodDB
spring.datasource.username=prod-db-user
spring.datasource.password=prod-db-passphrase

 

Spring Boot Active Profile

We can specify in our default "application.properties" which properties should Spring Boot application while starting the application.

spring.profiles.active=development

 

On application startup, the active profile will be picked and properties of the active profile will be given priority.

 

The most important point to remember, that default "application.properties" file will still be loaded. But when there is any property collision (a property specified in default application.properties and the same property is specified in the environment-specific properties file) in this situation properties of environment-specific property file will be given priority and loaded.

 

Best Practices While Handling Properties 

 

Identify which properties are common and you don't want different values for those properties in environments. Common properties MUST be kept ONLY in the default application.properties file.

 

@Value Annotation Usage

@Value annotation used to read environment or application property's value in Java code. Below is the syntax which reads the value of a property.

 

@Value("${property_key_name}")

 

YAML File alternative to application.properties file

 

As an alternate to application.properties file, Spring Boot also provides configuration with YAML File. YAML file serves the same purpose as "application.properties" it's just that there is a difference in systax/format. YAML also should be kept in "src/main/resources" directory.

 

YAML Sample File

spring:
   application:
      name: my-springboot-application
server:
   port: 8090
   servlet:
        context-path: /my-springboot-application
spring:
    profiles:
        active: developement

---
spring:
   profiles: development
spring:
    datasource:
        driver-class-name: com.mysql.jdbc.Driver
        password: root
        url: jdbc:mysql://localhost:3306/testDB
        username: root
    jpa:
        hibernate:
            ddl-auto: update
        properties:
            hibernate:
                dialect: org.hibernate.dialect.MySQL5InnoDBDialect
        show-sql: false

---
spring: 
   profiles: production
spring:
    datasource:
        driver-class-name: com.mysql.jdbc.Driver
        password: prod-db-passphrase
        url: jdbc:mysql://production-server:3306/prodDB
        username: prod-db-user
    jpa:
        hibernate:
            ddl-auto: none
        properties:
            hibernate:
                dialect: org.hibernate.dialect.MySQL5InnoDBDialect
        show-sql: true

 

Building Dynamic Applications with Spring and Java Frameworks

 

Spring framework allows for the easy and versatile development of web-based applications. We at Oodles AI are a team of developers with experiential knowledge in working with Spring, Java, Apache, React, Flutter, and other latest frameworks. Under AI, our capabilities include intelligent chatbot development, recommendation systems, computer vision applications, and predictive analytics services

 


About Author

Yasir Zuberi

Yasir is Lead Developer. He is a bright Java and Grails developer and have worked on development of various SaaS applications using Grails framework.

Request For Proposal

Sending message..

Ready to innovate ? Let's get in touch


Notice: Undefined index: HTTP_REFERER in /var/html/www/AI/wp-content/themes/oxides-child/functions.php on line 272

Notice: Undefined index: HTTP_REFERER in /var/html/www/AI/wp-content/themes/oxides-child/functions.php on line 272

Notice: Undefined index: HTTP_REFERER in /var/html/www/AI/wp-content/themes/oxides-child/functions.php on line 272

Notice: Undefined index: HTTP_REFERER in /var/html/www/AI/wp-content/themes/oxides-child/functions.php on line 272

Chat With Us