1. Overview

application.properties file contains all configuration properties in spring boot application. application.properties file contains spring boot properties as well as custom properties so during application development requires to read those configurations from application.properties file.

application.properties file must be at root location of resource folder.

Spring boot provides different ways to read application.properties files:

  1. Spring boot read application properties file using org.springframework.core.env.Environment
  2. Spring boot read application properties using @ConfigurationProperties annotation
  3. Spring boot read application properties using @Bean configuration
  4. Spring boot read application properties using @Value annotation

2. Example

Spring boot read application properties

Spring boot read application properties

1. Spring boot read application properties using Environment

1.1 application.properties

Environment represents current system configuration. Its provide getProperty method which returns provided valued based on the passed property name. It also support return Default Value if configuration return null value or configuration is not available. Here is an example of spring boot read application properties using Environment.

app.domain=javadeveloperzone.com
app.subdomain=api

1.2 ApplicationService

Here we have @Autowired at Service but according to requires we can autowire at Controller level also.

package com.javadeveloperzone.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

/**
 * Created by Java Developer Zone on 26-08-2017.
 */
@Service
public class ApplicationService {

    @Autowired
    private Environment environment;
    
    public void printProperties(){
        System.out.println(environment.getProperty("app.domain"));          // It will return properties value from application.properties file, If key will not available than it will return null
        System.out.println(environment.getProperty("app.subdomain"));
        System.out.println(environment.getProperty("app.rootsubdomain","Default Value")); // If value is null than return default value
    }
}

1.3 Output:

javadeveloperzone.com
api
Default Value

2. Spring boot read application properties using @Value annotation

@Value("${propertyName[:DEFAULT VALUE]}") is syntax to read properties from application.properties file. here is an example of read application properties using @Value annotation.

2.1 application.properties

app.domain=javadeveloperzone.com
app.subdomain=api

2.2 ApplicationService

Here we have used @Value at Service layer but we can also use at Controller and Component level.

package com.javadeveloperzone.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * Created by Java Developer Zone on 26-08-2017.
 */
@Service
public class ApplicationService {

    @Value("${app.domain}")
    private String appDomain;

    @Value("${app.subdomain}")
    private String appSubdomain;

    @Value("${app.rootsubdomain:Default value}")        // This properties is not available so it will return default value
    private String rootsubdomain;

    public void printProperties(){
        System.out.println("Domain: "+appDomain);          // It will return properties value from application.properties file, If key will not available than it will return null
        System.out.println("Sub Domain: "+appSubdomain);
        System.out.println("Root Domain: "+rootsubdomain);
    }
}

2.3 Output:

Domain: javadeveloperzone.com
Sub Domain: api
Root Domain: Default value

3. References:

 

Was this post helpful?
Let us know, if you liked the post. Only in this way, we can improve us.
Yes
No

Leave a Reply

Your email address will not be published. Required fields are marked *