🛠️ Inject Complex Data Types in Spring Boot Using @ConfigurationProperties

️-inject-complex-data-types-in-spring-boot-using-@configurationproperties

🛠️ Inject Complex Data Types in Spring Boot Using @ConfigurationProperties

Meta Description:
Learn how to inject arrays, lists, sets, maps, and nested objects into Spring Boot beans using @ConfigurationProperties with application.properties. Full working example included.

✅ Introduction

Spring Boot makes it incredibly easy to inject configuration values from .properties or .yml files using @ConfigurationProperties. But did you know you can also inject arrays, collections, maps, and nested Java objects?

In this blog post, we’ll walk through an example that demonstrates how to inject all of the above using application.properties.

🔍 Use Case Overview

Let’s assume we want to load employee data from the properties file, including:

  • Basic types (pid, pname)
  • Array (favColors)
  • List and Set (studies, phoneNumbers)
  • Map (idDetails)
  • Nested object (jobInfo)

⚙️ Step 1: Define the Java Beans

🔸 JobDetails.java – Nested Class

public class JobDetails {
    private String desg;
    private String company;
    private Integer deptNo;

    // Getters and Setters

    @Override
    public String toString() {
        return "JobDetails [desg=" + desg + ", company=" + company + ", deptNo=" + deptNo + "]";
    }
}

🔸 Employee.java – Main Config Bean

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

@Component
@ConfigurationProperties(prefix = "emp")
public class Employee {

    private Integer pid;
    private String pname;
    private String[] favColors;
    private List<String> studies;
    private Set<Long> phoneNumbers;
    private Map<String, Object> idDetails;
    private JobDetails jobInfo;

    // Getters and Setters
}

📝 Note: Spring uses setter injection, so don’t forget to include the setters (or use Lombok’s @Setter).

🏗️ Step 2: Define Properties in application.properties

# Primitive types
emp.pid=101
emp.pname=Sam

# Array
emp.fav-colors=Blue,Green,Red

# List
emp.studies=SSC,HSC,BE,MTech

# Set (same format as List; Spring ensures uniqueness)
emp.phone-numbers=9876543210,9876501234,9123456789

# Map
emp.id-details.pan=ABCDE1234F
emp.id-details.aadhar=123456789012
emp.id-details.passport=Z1234567

# Nested Object
emp.job-info.desg=Software Engineer
emp.job-info.company=Tech Solutions
emp.job-info.dept-no=101

🧪 Step 3: Test the Injected Configuration

@RestController
public class EmployeeController {

    private final Employee employee;

    public EmployeeController(Employee employee) {
        this.employee = employee;
    }

    @GetMapping("https://dev.to/emp-info")
    public Employee getEmployeeInfo() {
        return employee;
    }
}

✅ Output (Sample JSON Response)

{
  "pid": 101,
  "pname": "Sam",
  "favColors": ["Blue", "Green", "Red"],
  "studies": ["SSC", "HSC", "BE", "MTech"],
  "phoneNumbers": [9876543210, 9876501234, 9123456789],
  "idDetails": {
    "pan": "ABCDE1234F",
    "aadhar": "123456789012",
    "passport": "Z1234567"
  },
  "jobInfo": {
    "desg": "Software Engineer",
    "company": "Tech Solutions",
    "deptNo": 101
  }
}

📘 Bonus Tips

  • Add this dependency for metadata auto-complete in IDEs:

  org.springframework.boot
  spring-boot-configuration-processor
  true

  • You can use @Validated for validation on properties.
  • Use .yml instead of .properties for better readability if the structure is deep.

🎯 Conclusion

Using @ConfigurationProperties in Spring Boot is a clean, maintainable way to inject not just simple values but also complex data structures like lists, maps, and nested objects. It helps in keeping your configuration organized and strongly typed.

Total
0
Shares
Leave a Reply

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

Previous Post
sunday-rewind:-identify-and-manage-imposter-syndrome-by-phyllis-njoroge

Sunday Rewind: Identify and manage imposter syndrome by Phyllis Njoroge

Next Post
tariffs-trigger-shifts-in-supply-chains-and-quality-strategy

Tariffs Trigger Shifts in Supply Chains and Quality Strategy

Related Posts