πŸ› οΈ Inject Complex Data Types in Spring Boot Using @ConfigurationProperties

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MyrinNew
    Senior Member
    • Feb 2024
    • 5175

    #1

    πŸ› οΈ 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.Config urationProperties;
    import org.springframework.stereotype.Component;

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

    private Integer pid;
    private String pname;
    private String[] favColors;
    private ListString> studies;
    private SetLong> phoneNumbers;
    private MapString, 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("/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.




    More...
Working...