Here are different ways to read Spring boot different ways to read properties file using @ConfigurationProperties like convert properties to List,Map, read nested properties ect. Here is complete example of read spring boot read property value using @ConfigurationProperties.

1. Read property values to Map using @ConfigurationProperties

application.properties

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

AppProperties

package com.javadeveloperzone.model;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
/**
 * Created by Java Developer Zone on 26-08-2017.
 */
@ConfigurationProperties()       
@Component
public class AppProperties {
    private java.util.Map<String, String> app = new HashMap<>();  // it will store all properties start with app
    public Map<String, String> getApp() {
        return app;
    }
    public void setApp(Map<String, String> app) {
        this.app = app;
    }
}

2. Read property values to List using @ConfigurationProperties

application.properties

app.information[0]=Java
app.information[1]=Developer
app.information[2]=Zone

AppProperties

package com.javadeveloperzone.model;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
/**
 * Created by Java Developer Zone on 26-08-2017.
 */
@ConfigurationProperties(prefix = "app")       // It will read all properties prefix with app
@Component
public class AppProperties {
    private java.util.List<String>  information=new ArrayList<>();  // it will contains all value of app.information[0],app.information[1],app.information[2]
    public List<String> getInformation() {
        return information;
    }
    public void setInformation(List<String> information) {
        this.information = information;
    }
}

3. Read Nested Property to Sub Class using @ConfigurationProperties

application.properties

app.information[0]=Java
app.information[1]=Developer
app.information[2]=Zone
app.data.value=application value
app.data.basicValue=application basic value

AppProperties

package com.javadeveloperzone.model;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
/**
 * Created by Java Developer Zone on 26-08-2017.
 */
@ConfigurationProperties(prefix = "app")       // It will read all properties prefix with app
@Component
public class AppProperties {
    private java.util.List<String>  information=new ArrayList<>();
    private Data data;
    public Data getData() {
        return data;
    }
    public void setData(Data data) {
        this.data = data;
    }
    public static class Data{
        private String value;
        private String basicValue;
        public String getValue() {
            return value;
        }
        public void setValue(String value) {
            this.value = value;
        }
        public String getBasicValue() {
            return basicValue;
        }
        public void setBasicValue(String basicValue) {
            this.basicValue = basicValue;
        }
    }
    public List<String> getInformation() {
        return information;
    }
    public void setInformation(List<String> information) {
        this.information = information;
    }
}

4. References

Was this post helpful?

Leave a Reply

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