简体   繁体   中英

Inject values with Spring and YAML with nested properties

I want to inject some values from a YAML to the Spring context. The structure of the YAML is similar so I did not want to duplicate code, but the Spring startup is failing because it is not being able to inject the value to the placeholder.

Please note my application.properties :

server.port=8084

activeProfile=dev

autoAgents.supplier.id=0
autoAgents.supplier.name=test
autoAgents.supplier.serviceType=REST
autoAgents.supplier.authType=1
autoAgents.supplier.adapter=test
autoAgents.supplier.username=test
autoAgents.supplier.secret=test
autoAgents.supplier.apiPassword=12345

autoAgents.client.id=1
autoAgents.client.name=test
autoAgents.client.serviceType=REST
autoAgents.client.authType=1
autoAgents.client.adapter=
autoAgents.client.username=test
autoAgents.client.secret=test
autoAgents.client.apiPassword=12345

Then I am injecting this values on the YAML, application.yml

activeProfile: ${activeProfile}

autoAgents:
    supplier:
        isSupplier: true
        meta:
            id: ${autoAgents.supplier.id}
            name: ${autoAgents.supplier.name}
            serviceType: ${autoAgents.supplier.serviceType}
            authType: ${autoAgents.supplier.authType}
            adapter: ${autoAgents.supplier.adapter}
        credentials:
            username: ${autoAgents.supplier.username}
            secret: ${autoAgents.supplier.secret}
            apiPassword: ${autoAgents.supplier.apiPassword}
    client:
    isSupplier: false
    meta:
        id: ${autoAgents.client.id}
        name: ${autoAgents.client.name}
        serviceType: ${autoAgents.client.serviceType}
        authType: ${autoAgents.client.authType}
        adapter: ${autoAgents.client.adapter}
    credentials:
        username: ${autoAgents.client.username}
        secret: ${autoAgents.client.secret}
        apiPassword: ${autoAgents.client.apiPassword}

And then I am importing this to a configuration property context:

@Configuration
@EnableConfigurationProperties
@ConfigurationProperties    
@Data
public class TwoConnectConfigurationProperties {
    
    private String activeProfile;
    @Value("${autoAgents.supplier}")
    private AutoAgentDup supplier;
    @Value("${autoAgents.client}")
    private AutoAgentDup client;
}

But @Value("${autoAgents.supplier}") is not working.

Please advise.

As mentioned earlier it does not make sense to inject values to yaml, you can just create the "application.yaml" with the values directly. And just delete the ".properies" file.

You might want to take a look how to easily inject the properties with common suffix into a bean. Its nicely described here: https://www.baeldung.com/configuration-properties-in-spring-boot

You will have a bean:

@Configuration
@ConfigurationProperties(prefix = "autoAgents.supplier")
public class AutoAgentSupplierProperties {

  private long id;
  private String name;
  // ... rest of the properies properties
}

You might want the same for the "auto.agent" client.

If you want to avoid code duplication, you can have a bean with the common properties. Extend that class with 2 new classes. One for supplier and one for agent - and annotate those with

@ConfigurationProperties

annotation.

Why you need "nested properties"? If you only want to access them in application, just take values from.properties file and fill them as values to.yml file. Eg: profile: dev , or

autoAgents:
  client:
    id: 1

Properties from.yml file can be accessed from code same way as from.properties file.

Your problem is in way how you access properties. When you use "@Configuration properties", you have to specific which one to use (eg @ConfigurationProperties("autoAgents.client") .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM