首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >@ConfigurationProperties Set<Type>

@ConfigurationProperties Set<Type>
EN

Stack Overflow用户
提问于 2019-12-21 21:26:14
回答 2查看 601关注 0票数 2

我希望在启动时加载一些属性,并将它们转换为适当的类型。

application.yml:

代码语言:javascript
复制
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

代码语言:javascript
复制
@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,如下所示:

代码语言:javascript
复制
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$2(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.access$100(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)

解决方案:

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

代码语言:javascript
复制
    @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;
    }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-12-21 22:08:12

类是内部类,需要构造外部类BaseProperties的实例,Spring不支持这些实例。

您可以用关键字static标记类,该关键字应该可以工作。

代码语言:javascript
复制
@Getter
@Setter
public static class AuthorityProperty {
    private String name;
    private List<String> scopes;
}
票数 1
EN

Stack Overflow用户

发布于 2019-12-21 21:53:19

我刚刚弄清楚了这件事的原因。看起来,spring不能通过声明属性集的类型的内部类来处理这个问题。我将这些类提取成不同的类,但不起作用。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59439947

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档