

Table of Contents
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. We can also change the name and location of application.properties file here is details for same.
Make sure that once you update the application.properties file then need restart the application to apply those changes.
Spring boot provides multiple ways to read application.properties
or application.yml
files:
- Spring boot can read application properties file using
org.springframework.core.env.Environment
- 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. - Spring boot is also capable to read application properties using
@Bean
configuration @Value
annotation can also be used to read application.properties.
2. Example

Spring boot read application properties
1. Spring boot read application properties using Environment
1.1 application.properties
Environment represents current application 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.Conclusion
In this article, We have learned the different ways to reading application.properties or application.yml file in spring boot application. We can use any of those ways based on our choice and requirements.
3. References: