We know spring boot supports Profile-specific Properties Files , meaning we can has one app.properties
defining common/shared key-value pair and additional app-[prod|dev|stage].properties
. With spring.profiles.active
, we can switch different profile .
But I wonder if this is available in normal spring context WITHOUT spring-boot ?
The most important requirement : I don't want to hard-code profile in any Java/XML/Kotlin code (except in Tests , which may assign @ActiveProfiles("dev")
). Which means
This is not ideal :
@PropertySources(
PropertySource(value = ["classpath:app.properties"]),
PropertySource(value = ["classpath:app-dev.properties"], ignoreResourceNotFound = true),
)
as it hard-coded dev
in the code.
And this is not acceptable
PropertySourcesPlaceholderConfigurer().apply {
setLocations(
ClassPathResource("app.properties") ,
ClassPathResource("app-dev.properties")
)
}
as it also hard-coded dev
in the code.
Solution like :
<context:property-placeholder location="classpath:app-${spring.profiles.active}.properties" />
is not acceptable , as there will be an app-.properties
storing common/shared K/V , which is very ugly (the redundant -
character )
XML solutions are welcome , but such as
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>
is not so good. As it is not auto-discovery.
I hope it can auto discover relevant profiles , for example : app.properties
, app-prod.properties
, app-dev.properties
and automatically knows there are two profiles (prod
and dev
). With @ActiveProfiles("dev")
, it automatically picks up app.properties
and app-dev.properties
, and uses dev
to override default KVs. Just like what spring-boot does. But without boot environment.
This will be packed in WAR and deployed to Tomcat. So , no spring boot needed.
Environment : spring-core 5.3.13
Thanks.