考虑以下application.yml
:
shapes:
- type: "square"
side: 4
- type: "circle"
radius: 6
具有以下Java层次结构:
public abstract class Shape {
private String type;
...
}
public class Square extends Shape {
private float side;
...
}
public class Circle extends Shape {
private float radius;
...
}
在@ConfigurationProperties
类中使用基类不起作用:
@Configuration
@ConfigurationProperties
public class ShapesProperties {
// BindException
private List<Shape> shapes;
...
}
Spring是否支持这种情况?我已经读过ConversionService
,Binder
,Converter<S, T>
,但不确定是否可以解决此问题。