簡體   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