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 throws exception while starting of application.

Spring boot @ConfigurationProperties Example Project

Spring boot @ConfigurationProperties Example Project

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

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;
    }
}

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;
    }
}

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();
    }
}

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>
Spring boot @ConfigurationProperties Example Output

Spring boot @ConfigurationProperties Example Output

Here is different ways to read configuration properties file 

References:

Spring boot Document

 

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 *