基于环境的Spring PropertySource

基于环境的Spring PropertySource,spring,spring-boot,Spring,Spring Boot,我想在PropertySource值选项中使用动态占位符 这是为了能够为每个覆盖默认文件的环境创建一个文件。就像application.properties和application-dev.properties一样 当前设置: @PropertySource("classpath:ione.properties") 我想要点像这样的 @PropertySource("classpath:ione-{optionalEnvName}.properties") 从而读取--spring.prof

我想在
PropertySource
值选项中使用动态占位符

这是为了能够为每个覆盖默认文件的环境创建一个文件。就像
application.properties
application-dev.properties
一样

当前设置:

@PropertySource("classpath:ione.properties")
我想要点像这样的

@PropertySource("classpath:ione-{optionalEnvName}.properties")
从而读取
--spring.profiles.active=dev
选项

谢谢

在以下情况下运行:

-Dspring.profiles.active=dev
然后:

@PropertySource("classpath:ione-${spring.profiles.active}.properties")
应与以下设备一起运行:

-Dspring.profiles.active=dev
然后:

@PropertySource("classpath:ione-${spring.profiles.active}.properties")

应该行

这可能会有帮助:谢谢。但是,它没有显示如何在PropertySource注释中使用动态变量。@Profile注释要求每个属性文件具有多个类。这会让消费者感到困惑(谁不在乎哪一个)。查看此链接:这可能会有帮助:谢谢。但是,它没有显示如何在PropertySource注释中使用动态变量。@Profile注释要求每个属性文件具有多个类。这会让消费者感到困惑(谁不在乎哪一个)。查看此链接:谢谢。这确实有效。遗憾的是,spring处理
application.properties
文件的方式并不完全相同。我不能有一个文件:
ione.properties
和ione-
dev.properties
基本上。如果我不提供
spring.profiles.active
参数,spring会抱怨它找不到`ione-.properties`例如。
@PropertySource(value={“classpath:properties/ione.properties”,“classpath:properties/ione-${spring.profiles.active}.properties”})
例如,当运行没有任何配置文件集的应用程序时,该示例将抱怨文件
ione-.properties
不存在。@NicolasWidart有一种解决方法-只需使用:
@PropertySource(“类路径:ione-${spring.profiles.active}.properties”,ignoreResourceNotFound=true)
@NicolasWidart另一种可能是定义默认值:@PropertySource(“classpath:ione-${spring.profiles.active:default}.properties”),谢谢。这确实有效。遗憾的是,spring处理
application.properties
文件的方式并不完全相同。我不能有一个文件:
ione.properties
和ione-
dev.properties
基本上。如果我不提供
spring.profiles.active
参数,spring会抱怨它找不到`ione-.properties`例如。
@PropertySource(value={“classpath:properties/ione.properties”,“classpath:properties/ione-${spring.profiles.active}.properties”})
例如,当运行没有任何配置文件集的应用程序时,该示例将抱怨文件
ione-.properties
不存在。@NicolasWidart有一种解决方法-只需使用:
@PropertySource(“类路径:ione-${spring.profiles.active}.properties”,ignoreResourceNotFound=true)
@NicolasWidart另一种可能是定义默认值:@PropertySource(“classpath:ione-${spring.profiles.active:default}.properties”)