1. Overview

application.properties file contains all configuration properties of a spring boot application. application.properties also contains custom properties which might be useful for application.

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

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

  1. Spring boot can read application properties file using org.springframework.core.env.Environment
  2. Spring boot can also read application properties using @ConfigurationProperties annotation, here is an example to read application property using @ConfigurationProperties and another example can be found here.
  3. Spring boot is also capable to read application properties using @Bean configuration
  4. @Value annotation can also be used to read application.properties.

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. It provides a methodgetProperty which returns provided value based on the supplied property name. It may also return Default Value if configuration returns 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 used annotation@Autowired at Service level but as per requirement, we can also 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 reading application properties using @Value annotation.

2.1 application.properties

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

2.2 ApplicationService

Here we have used @Value annotation at Service layer but we can also use it 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 *