SEARCH BY

All

  • All
  • Java8
  • Spring

Dealing with nested properties from properties file with ConfigurationProperties in Spring Boot

Post a Comment

Introduction

We are going to learn how to deal with list, map and object type parameters with ConfigurationProperties
In this tutorial we are going to learn how complext data types like List, Maps and Objects can be bind with .properties by using @ConfigurationProeprties.
For better understaing we are going to consider an example with Peron Class, where Person has the following details, name, gender, age, adress, qualification and favourite books.
name is storing into String data type
gender is storing into String data type
age is storing into integer data type
address is storing in Address object ( with buildingName, street, pincode and state )
qualification as a Map type ( ug->BSC, PG->M.Tech etc...)

JavaBean/POJO class

package com.jai43.jsite.config;

import java.util.List;
import java.util.Map;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

@Component
@PropertySource("classpath:messages.properties")
@ConfigurationProperties("ceo")
public class PersonCEO {

 private String name;

 private String gender;

 private int age;

 Address address = new Address();

 List<String> books;

 public String getName() {
  return name;
 }

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

 public String getGender() {
  return gender;
 }

 public void setGender(String gender) {
  this.gender = gender;
 }

 public int getAge() {
  return age;
 }

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

 public Address getAddress() {
  return address;
 }

 public void setAddress(Address address) {
  this.address = address;
 }

 public List<String> getBooks() {
  return books;
 }

 public void setBooks(List<String> books) {
  this.books = books;
 }

 public Map<String, String> getQualification() {
  return qualification;
 }

 public void setQualification(Map<String, String> qualification) {
  this.qualification = qualification;
 }

 Map<String, String> qualification;

 public class Address {

  String buildingName;
  String street;

  String state;
  String pinCode;

  public String getBuildingName() {
   return buildingName;
  }

  public void setBuildingName(String buildingName) {
   this.buildingName = buildingName;
  }

  public String getStreet() {
   return street;
  }

  public void setStreet(String street) {
   this.street = street;
  }

  public String getState() {
   return state;
  }

  public void setState(String state) {
   this.state = state;
  }

  public String getPinCode() {
   return pinCode;
  }

  public void setPinCode(String pinCode) {
   this.pinCode = pinCode;
  }

 }

}

Output screen

Dealing with nested properties from properties file with ConfigurationProperties in Spring Boot
jaya
I love software designing, coding, writing and teaching...

Share this article

Related Posts

Post a Comment

Place your code in <em> </em> tags to send in comments | Do not add hyper links in commnet

Close

Subscribe our newsletter for the latest articles directly into your email inbox