有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

没有spring引导的java Profile特定属性文件?

我们知道spring boot支持特定于概要文件的属性文件,这意味着我们可以有一个app.properties定义公共/共享键值对和其他app-[prod|dev|stage].properties。使用spring.profiles.active,我们可以切换不同的配置文件

但我想知道,在正常的spring环境中,如果没有spring引导,这是否可用

最重要的要求是:我不想在任何Java/XML/Kotlin代码中硬编码配置文件(除了在测试中,它可能分配@ActiveProfiles("dev"))。也就是说

这并不理想:

@PropertySources(
  PropertySource(value = ["classpath:app.properties"]),
  PropertySource(value = ["classpath:app-dev.properties"], ignoreResourceNotFound = true),
)

因为它在代码中硬编码了dev

这是不可接受的

PropertySourcesPlaceholderConfigurer().apply {
  setLocations(
    ClassPathResource("app.properties") ,
    ClassPathResource("app-dev.properties")
  )
}

因为它也在代码中硬编码了dev

解决方案如下:

<context:property-placeholder location="classpath:app-${spring.profiles.active}.properties" />

不可接受,因为将有一个app-.properties存储公共/共享K/V,这非常难看(冗余-字符)

XML解决方案是受欢迎的,但例如

app-prod.xml
<beans profile="prod">
  <context:property-placeholder location="classpath:app.properties,classpath:app-prod.properties" />
</beans>
and
app-dev.xml
<beans profile="dev">
  <context:property-placeholder location="classpath:app.properties,classpath:app-dev.properties" />
</beans>

不太好。因为它不是自动发现

我希望它能够自动发现相关的配置文件,例如:app.propertiesapp-prod.propertiesapp-dev.properties,并自动知道有两个配置文件(proddev)。使用@ActiveProfiles("dev"),它会自动拾取app.propertiesapp-dev.properties,并使用dev覆盖默认KVs。就像弹簧靴一样。但是没有启动环境

这将在战争中打包并部署到Tomcat。因此,不需要弹簧靴

环境:弹簧芯5.3.13

谢谢


共 (0) 个答案