sku*_*mar 8 spring spring-boot
我在一个属性文件中有几个属性,我想继承到应用程序属性.如果春季靴子可以,请告知.
就像是
application.properties
----------------------
extends = otherproperty.properties
property1 = value1
property2= value2
property3 = value3
otherproperty.properties
------------------------
property4=value4
property5 = value5
Run Code Online (Sandbox Code Playgroud)
加载spring boot app时,我想要使用@Value注释加载所有5个属性并使其可用.spring boot会自动选择classpath中的application.properties,而我没有applicaitoncontext xml或任何属性加载器代码.
提前致谢.
ESa*_*ala 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
Run Code Online (Sandbox Code Playgroud)
Spring将翻译文件并像这样使用它:
application.properties # This is the main file
spring.profiles=p1,p2,p3
prop-main=hi
property1=patata
property2=catsup
property3=helloworld
Run Code Online (Sandbox Code Playgroud)
此解决方案有效,但您必须为每个组保留单独的文件.
还有另一种方法,您可以使用单个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
Run Code Online (Sandbox Code Playgroud)
您可以阅读参考文档,以获取有关使用YAML而不是属性以及有关多配置文件YAML文档的更多信息.
小智 3
我有同样的问题,并找到了mkyong的一个很好的解释解决方案。.porperties本教程解释了如何通过使用或文件来使用不同的扩展 Spring 配置文件.yaml。(与eSala提到的方法相同。)
https://www.mkyong.com/spring-boot/spring-boot-profile-based-properties-and-yaml-example/
| 归档时间: |
|
| 查看次数: |
15451 次 |
| 最近记录: |