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

Java 使用Spring和带有嵌套属性的YAML注入值,java,spring,spring-boot,yaml,Java,Spring,Spring Boot,Yaml,我想将一些值从YAML注入Spring上下文。 YAML的结构类似,因此我不想重复代码,但Spring启动失败,因为它无法将值注入占位符 请注意我的应用程序属性: server.port=8084 activeProfile=dev autoAgents.supplier.id=0 autoAgents.supplier.name=test autoAgents.supplier.serviceType=REST autoAgents.supplier.authType=1 autoAgen

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

请注意我的
应用程序属性

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}”)
不起作用

请告知。

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

autoAgents:
  client:
    id: 1
可以使用与.Properties文件相同的方式从代码访问.yml文件中的属性

您的问题在于如何访问属性。使用“@Configuration properties”时,必须指定要使用的属性(例如
@ConfigurationProperties(“autoAgents.client”)
为什么需要“嵌套属性”?如果您只想在应用程序中访问它们,只需从.properties文件中获取值并将其作为值填充到.yml文件。例如:
profile:dev
,或

autoAgents:
  client:
    id: 1
可以使用与.Properties文件相同的方式从代码访问.yml文件中的属性


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

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

您可能想看看如何轻松地将带有公共后缀的属性注入到bean中。下面对其进行了很好的描述:

您将有一个bean:

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

  private long id;
  private String name;
  // ... rest of the properies properties
}
对于“auto.agent”客户机,您可能需要相同的设置

若您想避免代码重复,可以使用一个具有公共属性的bean。使用两个新类扩展该类。一个用于供应商,一个用于代理,并使用

@配置属性


注释。

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

您可能想看看如何轻松地将带有公共后缀的属性注入到bean中。下面对其进行了很好的描述:

您将有一个bean:

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

  private long id;
  private String name;
  // ... rest of the properies properties
}
对于“auto.agent”客户机,您可能需要相同的设置

若您想避免代码重复,可以使用一个具有公共属性的bean。使用两个新类扩展该类。一个用于供应商,一个用于代理,并使用

@配置属性


注释。

我不明白您为什么要这样做,而不仅仅是在应用程序中添加属性。yml?如何,所以?请您详细说明一下?我是这个主题的初学者,不能完全理解您的意思。谢谢。您不需要application.properties和application.yml,只要使用一种格式并用值填充它,如果您仍然不能我不明白你为什么要这样做,而不仅仅是在应用程序中添加属性。yml?如何,所以?你能详细说明一下吗?我是这个主题的初学者,不能完全理解你的意思。谢谢。你不需要application.properties和application.yml,只要使用一种格式并用值填充,如果您仍然无法获取值,请尝试添加简单的测试字符串并检查其是否已获取。您能否详细说明这是如何将供应商和客户机autoagent从yml注入到@value上的java类中的?我不太理解您的解决方案。您能否详细说明这是如何注入供应商的从yml到@value上的java类的客户端autoagent?我不太理解您的解决方案