Spring启动application.properties扩展了另一个属性文件

时间:2015-10-21 15:28:07

标签: spring spring-boot

我在一个属性文件中有几个属性,我想继承到应用程序属性。如果春季靴子可以,请告知。

类似

find a abs = [x | (y, x) <- abs, a == y]

当春季启动应用加载时,我想要使用@Value注释加载所有5个属性并使其可用。 spring boot会自动选择classpath中的application.properties,而且我没有applyaitoncontext xml或任何属性加载器代码。

提前致谢。

3 个答案:

答案 0 :(得分:10)

您可以使用个人资料

使用application.properties个文件,您可以为每个Profile定义一个文件,如下所示:

application.properties # This is the main file
spring.profiles=p1,p2,p3
prop-main=hi

application-p1.properties # This is the file with p1 properties
property1=patata

application-p2.properties # This is the file with p2 properties
property2=catsup

application-p3.properties # This is the file with p3 properties
property3=helloworld

Spring将翻译文件并像这样使用它:

application.properties # This is the main file
spring.profiles=p1,p2,p3
prop-main=hi
property1=patata
property2=catsup
property3=helloworld

此解决方案有效,但您必须为每个组保留一个单独的文件。

还有另一种方法,您可以使用单个YAML文件而不是多个properties文件。只需将application.properties替换为application.yml并执行以下操作:

server:
    address: 192.168.1.100
---
spring:
    profiles: development
server:
    address: 127.0.0.1
---
spring:
    profiles: production
server:
    address: 192.168.1.120

您可以阅读参考文档,详细了解Using YAML instead of PropertiesMulti-profile YAML documents

答案 1 :(得分:6)

对我来说,spring.profiles.include=p1,p2,p3文件

中有application.properties个作品

答案 2 :(得分:0)

我有同样的问题,并通过 mkyong 找到了一个很好的解释解决方案。本教程介绍了如何使用.porperties.yaml文件来使用不同的扩展Spring配置文件。 (与eSala提到的方法相同。)

https://www.mkyong.com/spring-boot/spring-boot-profile-based-properties-and-yaml-example/

相关问题