'Polymorphic list elements in Spring configuration?
Is there an easy way to have polymorphic list elements in a configuration?
E.g. for the following application.yaml:
Vehicles:
one:
type: Bicycle
id: 4711
bell: ringring
two:
type: Car
id: 4712
horn: toot
I'd like Spring to create Java objects of these classes (grossly simplified):
List<Vehicle>
class Vehicle { int id; }
class Bicycle extends Vehicle { String bell; }
class Car extends Vehicle { String horn; }
The solutions I have seen require duplication of the classes' attributes in another class, which I'd like to avoid:
- The
Builderapproach is five years old, so maybe it's not the best answer anymore? - It may be possible to use a Converter approach that works on whatever Spring's representation of a non-String property is. I haven't found a working example yet, though.
I know I could make something happen by writing a
Converter, but any changes in theVehiclesubclasses would then require matching changes in theConverterso I don't like that; but is there a simpler way?
I know Spring's configuration annotations and what their purpose is, but I don't have full mastery of what they do exactly and how they interact, so please give me enough details to piece working code together, thanks ;-)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
