Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

338
Views
Spring @ConfigurationProperties inheritance/nesting

How do I load configuration to create a list of Shapes which inherits defaults and also support overriding?

Here is how my application.yml files looks like...

store:
  default:
    color: red
    size: 10

  shapes:
    - id: square
      size: 20

    - id: circle
      size: 30
      color: black

    - id: rectangle

And here is what I want...

{
  "catalog": {
    "shapes": [
      {
        "color": "red",    // default
        "size": 20,        // override
        "id": "square"
      },
      {
        "color": "black",  // override
        "size": 30,        // override
        "id": "circle"    
      },
      {
        "color": "red",    // default
        "size": 10,        // default
        "id": "rectangle"  
      }
    ]
  }
}

So far I have tried following but its missing defaults in the inheritance. In other words, the defaults never makes into Shapes object.

@lombok.Data
@Component
@ConfigurationProperties(prefix = "store")
public class Catalog {
    private List<Shape> shapes;   
}

@lombok.Data
public class Shape extends DefaultConfig {
    private String id;
}

@lombok.Data
@ConfigurationProperties(prefix = "store.default")
@Component
public class DefaultConfig {
    private String color;
    private int size;
}
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

There's no magic way to do that. The size must be an Integer and you should post-process your configuration to apply the defaults if need to be.

Something as easy as

public class Catalog {

    private final DefaultConfig defaultConfig;

    public Catalog(DefaultConfig defaultConfig) { ... }

    @PostConstruct   
    public void initialize() {
      // iterate over all the shapes and if the color or size is null
      // apply the default value from defalutConfig
    }
}
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!