Spring @ConfigurationProperties

Spring project initializer page (https://start.spring.io/)
Project structure in IntelliJ IDE.
pom.xml file for the project.
#DEV environment properties
dev.name=Development Application
dev.port
=8091
dev.dbtype
=Maria DB
dev.version
=1.0.alpha
dev.dburl
=jdbc:mariadb://localhost:3306/
dev.dbname
=studentDB
dev.dbuser
=root
dev.dbpassword
=root

#QA environment properties
qa.name=QA Test Application
qa.port
=8092
qa.dbtype
=Mongo DB
qa.version
=1.2.beta
qa.dburl
=mongodb://mongodb0.example.com:27017/
qa.dbname
=studentDB
qa.dbuser
=admin
qa.dbpassword
=admin

#PROD environment properties
prod.name=Production Application
prod.port
=8091
prod.dbtype
=MySQL
prod.version
=1.0.1
prod.dburl
=jdbc:mysql://localhost:3308/
prod.dbname
=studentDB
prod.dbuser
=admin-prod
prod.dbpassword
=admin-prod
Three classes for properties configuration
//DevConfigProps.javapackage com.habeebcycle.configurationpropertiesannotation.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConfigurationProperties("dev")
public class DevConfigProps {

}
//QaConfigProps.javapackage com.habeebcycle.configurationpropertiesannotation.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConfigurationProperties("qa")
public class QaConfigProps {

}
//ProdConfigProps.javapackage com.habeebcycle.configurationpropertiesannotation.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConfigurationProperties("prod")
public class ProdConfigProps {

}
package com.habeebcycle.configurationpropertiesannotation.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConfigurationProperties("dev")
public class DevConfigProps {

private String name;
private int port;
private String dbType;
private String version;
private String dbUrl;
private String dbName;
private String dbUser;
private String dbPassword;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getPort() {
return port;
}

public void setPort(int port) {
this.port = port;
}

public String getDbType() {
return dbType;
}

public void setDbType(String dbType) {
this.dbType = dbType;
}

public String getVersion() {
return version;
}

public void setVersion(String version) {
this.version = version;
}

public String getDbUrl() {
return dbUrl;
}

public void setDbUrl(String dbUrl) {
this.dbUrl = dbUrl;
}

public String getDbName() {
return dbName;
}

public void setDbName(String dbName) {
this.dbName = dbName;
}

public String getDbUser() {
return dbUser;
}

public void setDbUser(String dbUser) {
this.dbUser = dbUser;
}

public String getDbPassword() {
return dbPassword;
}

public void setDbPassword(String dbPassword) {
this.dbPassword = dbPassword;
}
}
package com.habeebcycle.configurationpropertiesannotation.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConfigurationProperties("qa")
public class QaConfigProps {

private String name;
private int port;
private String dbType;
private String version;
private String dbUrl;
private String dbName;
private String dbUser;
private String dbPassword;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getPort() {
return port;
}

public void setPort(int port) {
this.port = port;
}

public String getDbType() {
return dbType;
}

public void setDbType(String dbType) {
this.dbType = dbType;
}

public String getVersion() {
return version;
}

public void setVersion(String version) {
this.version = version;
}

public String getDbUrl() {
return dbUrl;
}

public void setDbUrl(String dbUrl) {
this.dbUrl = dbUrl;
}

public String getDbName() {
return dbName;
}

public void setDbName(String dbName) {
this.dbName = dbName;
}

public String getDbUser() {
return dbUser;
}

public void setDbUser(String dbUser) {
this.dbUser = dbUser;
}

public String getDbPassword() {
return dbPassword;
}

public void setDbPassword(String dbPassword) {
this.dbPassword = dbPassword;
}
}
package com.habeebcycle.configurationpropertiesannotation.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConfigurationProperties("prod")
public class ProdConfigProps {

private String name;
private int port;
private String dbType;
private String version;
private String dbUrl;
private String dbName;
private String dbUser;
private String dbPassword;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getPort() {
return port;
}

public void setPort(int port) {
this.port = port;
}

public String getDbType() {
return dbType;
}

public void setDbType(String dbType) {
this.dbType = dbType;
}

public String getVersion() {
return version;
}

public void setVersion(String version) {
this.version = version;
}

public String getDbUrl() {
return dbUrl;
}

public void setDbUrl(String dbUrl) {
this.dbUrl = dbUrl;
}

public String getDbName() {
return dbName;
}

public void setDbName(String dbName) {
this.dbName = dbName;
}

public String getDbUser() {
return dbUser;
}

public void setDbUser(String dbUser) {
this.dbUser = dbUser;
}

public String getDbPassword() {
return dbPassword;
}

public void setDbPassword(String dbPassword) {
this.dbPassword = dbPassword;
}
}
package com.habeebcycle.configurationpropertiesannotation;

import com.habeebcycle.configurationpropertiesannotation.config.DevConfigProps;
import com.habeebcycle.configurationpropertiesannotation.config.ProdConfigProps;
import com.habeebcycle.configurationpropertiesannotation.config.QaConfigProps;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Configuration;

@Configuration
public class PropertiesConfig implements CommandLineRunner {

private final DevConfigProps devConfigProps;
private final QaConfigProps qaConfigProps;
private final ProdConfigProps prodConfigProps;

private static final Logger logger = LoggerFactory.getLogger(ConfigurationPropertiesAnnotationApplication.class);

@Autowired
public PropertiesConfig(DevConfigProps devConfigProps, QaConfigProps qaConfigProps, ProdConfigProps prodConfigProps) {
this.devConfigProps = devConfigProps;
this.qaConfigProps = qaConfigProps;
this.prodConfigProps = prodConfigProps;
}


@Override
public void run(String... args) throws Exception {
logger.info("-------------Dev Properties-----------------");
logger.info("Name: {}", devConfigProps.getName());
logger.info("Port: {}", devConfigProps.getPort());
logger.info("DB Type: {}", devConfigProps.getDbType());
logger.info("Version: {}", devConfigProps.getVersion());
logger.info("DB Url: {}", devConfigProps.getDbUrl());
logger.info("DB name: {}", devConfigProps.getDbName());
logger.info("DB User: {}", devConfigProps.getDbUser());
logger.info("DB Password: {}", devConfigProps.getDbPassword());

logger.info("-------------QA Properties-----------------");
logger.info("Name: {}", qaConfigProps.getName());
logger.info("Port: {}", qaConfigProps.getPort());
logger.info("DB Type: {}", qaConfigProps.getDbType());
logger.info("Version: {}", qaConfigProps.getVersion());
logger.info("DB Url: {}", qaConfigProps.getDbUrl());
logger.info("DB name: {}", qaConfigProps.getDbName());
logger.info("DB User: {}", qaConfigProps.getDbUser());
logger.info("DB Password: {}", qaConfigProps.getDbPassword());

logger.info("-------------Prod Properties---------------");
logger.info("Name: {}", prodConfigProps.getName());
logger.info("Port: {}", prodConfigProps.getPort());
logger.info("DB Type: {}", prodConfigProps.getDbType());
logger.info("Version: {}", prodConfigProps.getVersion());
logger.info("DB Url: {}", prodConfigProps.getDbUrl());
logger.info("DB name: {}", prodConfigProps.getDbName());
logger.info("DB User: {}", prodConfigProps.getDbUser());
logger.info("DB Password: {}", prodConfigProps.getDbPassword());
}
}
---------------------Dev Properties-------------------------
2020-03-01 17:26:25.625 [main]: Name: Development Application
2020-03-01 17:26:25.626 [main]: Port: 8091
2020-03-01 17:26:25.626 [main]: DB Type: Maria DB
2020-03-01 17:26:25.626 [main]: Version: 1.0.alpha
2020-03-01 17:26:25.626 [main]: DB Url: jdbc:mariadb://localhost:3306/
2020-03-01 17:26:25.626 [main]: DB name: studentDB
2020-03-01 17:26:25.626 [main]: DB User: root
2020-03-01 17:26:25.627 [main]: DB Password: root
---------------------QA Properties-------------------------
2020-03-01 17:26:25.627 [main]: Name: QA Test Application
2020-03-01 17:26:25.627 [main]: Port: 8092
2020-03-01 17:26:25.627 [main]: DB Type: Mongo DB
2020-03-01 17:26:25.627 [main]: Version: 1.2.beta
2020-03-01 17:26:25.627 [main]: DB Url: mongodb://mongodb0.example.com:27017/
2020-03-01 17:26:25.627 [main]: DB name: studentDB
2020-03-01 17:26:25.627 [main]: DB User: admin
2020-03-01 17:26:25.627 [main]: DB Password: admin
---------------------Prod Properties-------------------------
2020-03-01 17:26:25.627 [main]: Name: Production Application
2020-03-01 17:26:25.627 [main]: Port: 8091
2020-03-01 17:26:25.627 [main]: DB Type: MySQL
2020-03-01 17:26:25.628 [main]: Version: 1.0.1
2020-03-01 17:26:25.628 [main]: DB Url: jdbc:mysql://localhost:3308/
2020-03-01 17:26:25.628 [main]: DB name: studentDB
2020-03-01 17:26:25.628 [main]: DB User: admin-prod
2020-03-01 17:26:25.628 [main]: DB Password: admin-prod
#application.properties
dev.mylist=SUN,MON,TUE,WED,THU,FRI,SAT
package com.habeebcycle.configurationpropertiesannotation.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.util.List;

@Configuration
@ConfigurationProperties("dev")
public class DevConfigProps {
private List<String> myList;

public List<String> getMyList() {
return myList;
}

public void setMyList(List<String> myList) {
this.myList = myList;
}

}
//Logging out myList will give
// [SUN, MON, TUE, WED, THU, FRI, SAT]
#application.propertiesdev.db.url=jdbc:mysql://localhost:3308/
dev.db.name
=student_marks
dev.db.user
=root
dev.db.password
=root
package com.habeebcycle.configurationpropertiesannotation.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.util.Map;

@Configuration
@ConfigurationProperties("dev")
public class DevConfigProps {
private Map<String, String> db;

public Map<String, String> getDb() {
return db;
}

public void setDb(Map<String, String> db) {
this.db = db;
}

}
{
"url": "jdbc:mysql://localhost:3308/",
"name": "student_marks",
"user": "root",
"password": "root"
}
package com.habeebcycle.configurationpropertiesannotation.model;

public class Student {

private String id;
private String name;
private int age;
private String level;
private double mark;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public String getLevel() {
return level;
}

public void setLevel(String level) {
this.level = level;
}

public double getMark() {
return mark;
}

public void setMark(double mark) {
this.mark = mark;
}
}
#application.propertiesdev.student.id=ABC123XYZ
dev.student.name
=Habeeb Okunade
dev.student.age
=34
dev.student.level
=700J
dev.student.mark
=99.4
package com.habeebcycle.configurationpropertiesannotation.config;

import com.habeebcycle.configurationpropertiesannotation.model.Student;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;


@Configuration
@ConfigurationProperties("dev")
public class DevConfigProps {

private Student student;

public Student getStudent() {
return student;
}

public void setStudent(Student student) {
this.student = student;
}

}
{
"id": "ABC123XYZ",
"name": "Habeeb Okunade",
"age": 34,
"level": "700J",
"mark": 99.4
}
package com.habeebcycle.configurationpropertiesannotation.model;

public class School{

private String id;
private String name;
private int size;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getSize() {
return size;
}

public void setSize(int size) {
this.size = size;
}

}
package com.habeebcycle.configurationpropertiesannotation.config;@Configuration
public class ExternalConfigProperties {

@Bean
@ConfigurationProperties("school")
public School school() {
return new School();
}
}
package com.habeebcycle.configurationpropertiesannotation.config;

import org.hibernate.validator.constraints.Length;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;

@Configuration
@ConfigurationProperties("validate")
@Validated
public class ConfigValidation {

@NotNull
private String dbUrl;

@Length(min = 4, max = 10)
private String dbUser;

@Min(8080)
@Max(8100)
private int dbPort;

@Pattern(regexp = "^[a-z0-9._%+-]+@[a-z0-9.-]+\\.[a-z]{2,6}$")
private String adminEmail;

public String getDbUrl() {
return dbUrl;
}

public void setDbUrl(String dbUrl) {
this.dbUrl = dbUrl;
}

public String getDbUser() {
return dbUser;
}

public void setDbUser(String dbUser) {
this.dbUser = dbUser;
}

public int getDbPort() {
return dbPort;
}

public void setDbPort(int dbPort) {
this.dbPort = dbPort;
}

public String getAdminEmail() {
return adminEmail;
}

public void setAdminEmail(String adminEmail) {
this.adminEmail = adminEmail;
}
}

--

--

Innovative software development professional. Loves sharing knowledge and learning latest stacks. Expertise in microservices architecture using spring boot.

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Habeeb Okunade

Habeeb Okunade

Innovative software development professional. Loves sharing knowledge and learning latest stacks. Expertise in microservices architecture using spring boot.