简体   繁体   English

使用具有嵌套属性的 Spring 和 YAML 注入值

[英]Inject values with Spring and YAML with nested properties

I want to inject some values from a YAML to the Spring context.我想将 YAML 中的一些值注入到 Spring 上下文中。 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. YAML 的结构类似,所以我不想复制代码,但 Spring 启动失败,因为它无法将值注入占位符。

Please note my application.properties :请注意我的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然后我将这个值注入到 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.但是@Value("${autoAgents.supplier}")不起作用。

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.如前所述,将值注入 yaml 是没有意义的,您可以直接使用这些值创建“application.yaml”。 And just delete the ".properies" file.只需删除“.properies”文件。

You might want to take a look how to easily inject the properties with common suffix into a bean.您可能想看看如何轻松地将具有通用后缀的属性注入到 bean 中。 Its nicely described here: https://www.baeldung.com/configuration-properties-in-spring-boot它在这里得到了很好的描述: 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.对于“auto.agent”客户端,您可能希望相同。

If you want to avoid code duplication, you can have a bean with the common properties.如果你想避免代码重复,你可以拥有一个具有通用属性的 bean。 Extend that class with 2 new classes.使用 2 个新类扩展 class。 One for supplier and one for agent - and annotate those with一份用于供应商,一份用于代理 - 并用

@ConfigurationProperties @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.如果您只想在应用程序中访问它们,只需从 .properties 文件中获取值并将它们作为值填充到 .yml 文件中。 Eg: profile: dev , or例如: profile: dev

autoAgents:
  client:
    id: 1

Properties from.yml file can be accessed from code same way as from.properties file.可以从代码访问来自.yml 文件的属性,方式与来自.properties 文件的方式相同。

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") .当您使用“@Configuration 属性”时,您必须指定使用哪一个(例如@ConfigurationProperties("autoAgents.client")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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