我希望在启动时加载一些属性,并将它们转换为适当的类型。
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$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)解决方案:
将类标记为静态或将它们创建为单独的类
@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;
}发布于 2019-12-21 22:08:12
类是内部类,需要构造外部类BaseProperties的实例,Spring不支持这些实例。
您可以用关键字static标记类,该关键字应该可以工作。
@Getter
@Setter
public static class AuthorityProperty {
private String name;
private List<String> scopes;
}发布于 2019-12-21 21:53:19
我刚刚弄清楚了这件事的原因。看起来,spring不能通过声明属性集的类型的内部类来处理这个问题。我将这些类提取成不同的类,但不起作用。
https://stackoverflow.com/questions/59439947
复制相似问题