繁体   English   中英

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

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

我想将 YAML 中的一些值注入到 Spring 上下文中。 YAML 的结构类似,所以我不想复制代码,但 Spring 启动失败,因为它无法将值注入占位符。

请注意我的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

然后我将这个值注入到 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}

然后我将其导入配置属性上下文:

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

但是@Value("${autoAgents.supplier}")不起作用。

请指教。

如前所述,将值注入 yaml 是没有意义的,您可以直接使用这些值创建“application.yaml”。 只需删除“.properies”文件。

您可能想看看如何轻松地将具有通用后缀的属性注入到 bean 中。 它在这里得到了很好的描述: https://www.baeldung.com/configuration-properties-in-spring-boot

你会得到一个豆子:

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

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

对于“auto.agent”客户端,您可能希望相同。

如果你想避免代码重复,你可以拥有一个具有通用属性的 bean。 使用 2 个新类扩展 class。 一份用于供应商,一份用于代理 - 并用

@ConfigurationProperties

注解。

为什么需要“嵌套属性”? 如果您只想在应用程序中访问它们,只需从 .properties 文件中获取值并将它们作为值填充到 .yml 文件中。 例如: profile: dev

autoAgents:
  client:
    id: 1

可以从代码访问来自.yml 文件的属性,方式与来自.properties 文件的方式相同。

您的问题在于您访问属性的方式。 当您使用“@Configuration 属性”时,您必须指定使用哪一个(例如@ConfigurationProperties("autoAgents.client")

暂无
暂无

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

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