多态@ConfigurationProperties

时间:2019-02-16 16:55:29

标签: java spring-boot polymorphism yaml deserialization

考虑以下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是否支持这种情况?我已经读过ConversionServiceBinderConverter<S, T>,但不确定是否可以解决此问题。

0 个答案:

没有答案