@ConfigurationProperties 设置<Type>

@ConfigurationProperties Set<Type>

我想让 spring 在启动时加载一些属性并将它们转换为正确的类型。

application.yml:

base:
  security:

    scopes:
      - name: read
        descripton: read some nice things

      - name: write
        description: write some nice things

    authorities:
      - name: ROLE_ADMIN
        scopes:
          - read
          - write

      - name: ROLE_USER
        scopes:
          - read 

为了将这些属性加载到类型中,我使用了以下@ConfigurationProperties: BaseProperties.java

@Setter
@Getter
@Configuration
@ConfigurationProperties(prefix="base.security")
public class BaseProperties {

    private Set<ScopeProperty> scopes = new HashSet<>();
    private Set<AuthorityProperty> authorities = new HashSet<>();

    @Getter
    @Setter
    public class AuthorityProperty {
        private String name;
        private List<String> scopes;
    }

    @Getter
    @Setter
    public class ScopeProperty {
        private String name;
        private String description;
    }
}

我得到的一切都是 BindException,如下所示:

Caused by: org.springframework.boot.context.properties.bind.UnboundConfigurationPropertiesException: The elements [base.security.authorities[0].name,base.security.authorities[0].scopes[0],base.security.authorities[0].scopes[1],base.security.authorities[1].name,base.security.authorities[1].scopes[0]] were left unbound.
    at org.springframework.boot.context.properties.bind.IndexedElementsBinder.assertNoUnboundChildren(IndexedElementsBinder.java:136)
    at org.springframework.boot.context.properties.bind.IndexedElementsBinder.bindIndexed(IndexedElementsBinder.java:113)
    at org.springframework.boot.context.properties.bind.IndexedElementsBinder.bindIndexed(IndexedElementsBinder.java:86)
    at org.springframework.boot.context.properties.bind.IndexedElementsBinder.bindIndexed(IndexedElementsBinder.java:71)
    at org.springframework.boot.context.properties.bind.CollectionBinder.bindAggregate(CollectionBinder.java:49)
    at org.springframework.boot.context.properties.bind.AggregateBinder.bind(AggregateBinder.java:56)
    at org.springframework.boot.context.properties.bind.Binder.lambda$bindAggregate(Binder.java:293)
    at org.springframework.boot.context.properties.bind.Binder$Context.withIncreasedDepth(Binder.java:429)
    at org.springframework.boot.context.properties.bind.Binder$Context.access0(Binder.java:372)
    at org.springframework.boot.context.properties.bind.Binder.bindAggregate(Binder.java:293)
    at org.springframework.boot.context.properties.bind.Binder.bindObject(Binder.java:254)
    at org.springframework.boot.context.properties.bind.Binder.bind(Binder.java:214)

解决方法:

将类标记为静态将它们创建为单独的类:

    @Getter
    @Setter
    public static class AuthorityProperty {
        private String name;
        private List<String> scopes;
    }

    @Getter
    @Setter
    public static class ScopeProperty {
        private String name;
        private String description;
    }

我刚刚弄清楚这个问题的原因。 似乎 spring 无法通过内部 类 声明 属性 集的类型来处理此问题。 我将 类 提取为单独的,但没有用。

您的 class 是内部 class ,需要构造外部 class BaseProperties 的实例,Spring Properties 不支持。

您可以使用关键字 static 标记您的 class,这应该有效

@Getter
@Setter
public static class AuthorityProperty {
    private String name;
    private List<String> scopes;
}