属性 spring 引导中的文件读取错误

Property file reading error in spring boot

下面是我在 spring boot app

中的 yml 文件
stack:
  apiContext: data-service
  domainOwner: trata
  domainContext: Madata-service
  #cluster:
   # servicePort: 12000
    #gossipPort: 13000
    #seedName: seed-service
   # seeds:
  #    - localhost:14000
  cluster:
    servicePort: 21000
    gossipPort: 15000
#    seedName: seed-service
    seeds:
      - localhost:13000
  providers:
    com.cloudimpl.out.collection.CollectionProvider:
      - name: MemCollection
        impl: com.cloudimpl.outstack.collection.MemCollectionProvider
        status: active

      - name: com.cloudimpl.outstack.collection.CollectionProvider
        impl: com.cloudimpl.outstack.collection.AwsCollectionProvider
        #        status: active
        configs:
          endpoint: http://localhost:1234
          leaderTable: LeaderTable
    com.cloudimpl.outstack.runtime.EventRepositoryFactory:
      - name: MemRepositoryFactory
        impl: com.cloudimpl.outstack.runtime.repo.MemEventRepositoryFactory


      - name: PostgresRepositoryFactory
        impl: com.cloudimpl.outstack.spring.repo.PostgresRepositoryFactory
        status: active
        configs:
          jdbcUrl: jdbc:postgresql://localhost:5432/test
          username: postgres
          password: QazWsx@1234
          defaultTable: masterdataService
          #UserTable: userTable
server:
  port: 9096
spring:
  main:
    web-application-type: reactive

低于该值访问是工作文件

@Value("${outstack.domainOwner}")
private String abc;

但是当我尝试以下面的方式访问时出现错误。

@Value("${outstack.providers.com.cloudimpl.outstack.runtime.EventRepositoryFactory.name}")
private String abc;

我检查过它只在添加“com.cloudimpl.outstack.runtime.EventRepositoryFactory”这部分时出错。

我该如何解决这个问题?

这样不行。您有 list,其值低于“outstack.providers.com.cloudimpl.outstack.runtime.EventRepositoryFactory”。这意味着您可以像这样添加变量:

@Value("${outstack.providers.com.cloudimpl.outstack.runtime.EventRepositoryFactory}")
private List<ParamWrapper> abc;

class ParamWrapper {

private String name;
private String impl;

....
getters and setters
}

然后在此列表中检查您的参数。

首先,我看到 stack.providers... 而不是 outstack.providers...

您需要了解的第二件事是,stack.providers.com.cloudimpl.outstack.runtime.EventRepositoryFactory 包含元素数组。所以你的 @Value(...) 不会起作用。

你可以做的是创建一个单独的 @ConfigurationProperties class 如下,

@Configuration
@ConfigurationProperties(prefix = "stack.providers.com.cloudimpl.outstack.runtime")
public class EvenConfigProperties {
    @JsonProperty("EventRepositoryFactory")
    private List<Config> eventRepositoryFactory;

    // getters and setters
}
public class Config {
    private String name;
    private String impl;
    // other parameters
    
    // getters and setters
}