Home > OS >  Property file reading error in spring boot
Property file reading error in spring boot

Time:10-01

Below is my yml file in spring boot app

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

below value access is working file

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

but when I try to access in below way it gives an error.

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

I checked it gives error only when add "com.cloudimpl.outstack.runtime.EventRepositoryFactory" this part.

How can I solve this??

CodePudding user response:

It doesn't work so. You have list with values under "outstack.providers.com.cloudimpl.outstack.runtime.EventRepositoryFactory". It means you can add variable like so:

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

class ParamWrapper {

private String name;
private String impl;

....
getters and setters
}

And then check your params in this list.

CodePudding user response:

First thing is, I see stack.providers... not outstack.providers...

Second thing you need to understand is that, stack.providers.com.cloudimpl.outstack.runtime.EventRepositoryFactory contains array of elements. So your @Value(...) won't work.

What you can do is create a separate @ConfigurationProperties class as follows,

@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
}
  • Related