0

我正在尝试从 Spring Boot 2.4 应用程序中的 application.properties 加载一个数组,其中该数组分布在多个属性文件中。

  1. 应用程序属性
spring.cloud.routes[0].id=1
spring.cloud.routes[0].name=ONE
spring.cloud.routes[1].id=2
spring.cloud.routes[1].name=TWO
spring.config.import=one.properties
  1. one.properties
spring.cloud.routes[2].id=3
spring.cloud.routes[2].name=THREE
spring.cloud.routes[3].id=4
spring.cloud.routes[3].name=FOUR
  1. Java Bean
    @Getter
    @Setter
    public class Route {
        private String id;
        private String name;
    }
    
    @Getter
    @Setter
    @ConfigurationProperties(prefix = "spring.cloud")
    @Component
    public class Routes {
        private List<Route> routes;
    }

    

在启动应用程序时,我遇到以下错误 -

***************************
APPLICATION FAILED TO START
***************************

Description:

Binding to target [Bindable@1591271 type = java.util.List<Route>, value = 'provided', annotations = array<Annotation>[[empty]]] failed:

    Property: spring.cloud.routes[2].id
    Value: 3
    Origin: class path resource [one.properties] - 1:27
    Reason: The elements [spring.cloud.routes[2].id,spring.cloud.routes[2].name,spring.cloud.routes[3].id,spring.cloud.routes[3].name] were left unbound.
    Property: spring.cloud.routes[2].name
    Value: THREE
    Origin: class path resource [one.properties] - 2:29
    Reason: The elements [spring.cloud.routes[2].id,spring.cloud.routes[2].name,spring.cloud.routes[3].id,spring.cloud.routes[3].name] were left unbound.
    Property: spring.cloud.routes[3].id
    Value: 4
    Origin: class path resource [one.properties] - 3:27
    Reason: The elements [spring.cloud.routes[2].id,spring.cloud.routes[2].name,spring.cloud.routes[3].id,spring.cloud.routes[3].name] were left unbound.
    Property: spring.cloud.routes[3].name
    Value: FOUR
    Origin: class path resource [one.properties] - 4:29
    Reason: The elements [spring.cloud.routes[2].id,spring.cloud.routes[2].name,spring.cloud.routes[3].id,spring.cloud.routes[3].name] were left unbound.

Action:

Update your application's configuration

有什么方法可以使这项工作无需在 java 注释中明确提及 one.properties 文件(以及除 application.properties 文件之外的任何其他附加文件)吗?

问候
雅各布

4

0 回答 0