

Table of Contents
1. Overview
Spring boot provide @ConfigurationProperties
annotation using that we can read application properties easily. Please note that application.properties file must be available at root location of resources
folder.
@ConfigurationProperties
also support to validate properties file while reading by spring boot application. If properties configuration is invalid than application will throw an exception while starting of application.
2. Example

Spring boot @ConfigurationProperties Example Project
2.1 application.properties
Here is some custom configuration in application.properties. While will be read using @ConfigurationProperties.
app.domain=javadeveloperzone.com app.subdomain=api app.domainCount=10
2.2 AppProperties
AppProperties contains @ConfigurationProperties(prefix = "app")
annotation so it will try to find out declare data member value from application.properties file and fetch it’s value.
NOTO : Name of properties and class member name must be same other and also require set and get methods for those member.
package com.javadeveloperzone.model; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; /** * Created by Java Developer Zone on 26-08-2017. */ @ConfigurationProperties(prefix = "app") // It will read all properties prefix with app @Component // declare as component to allowed autowite public class AppProperties { private String domain; // it will contains app.domain propery value private String subdomain; // it will contains app.subdomain propery value private int domainCount; // it will contains app.domainCount propery value public String getDomain() { return domain; } public String getSubdomain() { return subdomain; } public void setDomain(String domain) { this.domain = domain; } public void setSubdomain(String subdomain) { this.subdomain = subdomain; } public int getDomainCount() { return domainCount; } public void setDomainCount(int domainCount) { this.domainCount = domainCount; } }
2.3 ApplicationService
Here AppProperties has been autowired and return its value.
package com.javadeveloperzone.service; import com.javadeveloperzone.model.AppProperties; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; /** * Created by Java Developer Zone on 26-08-2017. */ @Service public class ApplicationService { @Autowired private AppProperties appProperties; public AppProperties getAppProperties(){ return appProperties; } }
2.4 AppPropertiesController
package com.javadeveloperzone.controller; import com.javadeveloperzone.model.AppProperties; import com.javadeveloperzone.service.ApplicationService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * Created by Lenovo on 19-07-2017. */ @RestController public class AppPropertiesController { @Autowired private ApplicationService applicationService; @RequestMapping("/getAppProperties") // It will return properties from application.properties file public AppProperties hello() { return applicationService.getAppProperties(); } }
2.5 pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>spring-boot-example</groupId> <artifactId>Spring-boot-ConfigurationProperties-Example</artifactId> <version>1.0-SNAPSHOT</version> <!-- Inherit defaults from Spring Boot --> <description>Spring boot @ConfigurationProperties Example</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-properties-files</artifactId> <version>1.5.4.RELEASE</version> </parent> <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <!-- Package as an executable jar --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2.6 Output

Spring boot @ConfigurationProperties Example Output
Here is different ways to read configuration properties file
3. References