问题描述
我在资源目录下的 application.properties 中保存了一些所有环境通用的属性(例如 spring.jpa.properties.hibernate.ejb.interceptor
).
I have few properties common to all environments (Ex. spring.jpa.properties.hibernate.ejb.interceptor
) which I have kept in application.properties under resource directory.
我在基于环境的属性文件中定义了数据库属性,我在启动应用程序时通过命令行从外部传递这些属性:
I have DB properties defined in environment based properties file which I pass externally through command line while starting the app:
java -jar -Dspring.config.location=<path-to-file> mySpringBootProject.jar
但是,当我从外部传递属性文件时,没有设置 spring.jpa.properties.hibernate.ejb.interceptor
.
However, spring.jpa.properties.hibernate.ejb.interceptor
is not being set when I am passing properties file externally.
我是否需要在外部文件中定义通用属性?或者有没有一种方法可以在一个地方定义它们,在不被覆盖时重复使用?
Do I need to define common properties even in external file? Or is there a way I can define them in a single place which is reused when not overridden?
推荐答案
您可以使用多个配置文件来执行此操作.例如,创建属性文件:
You can use multiple profiles to do this. For example, create property files:
application-dev.properties
application-prod.properties
application.properties
将特定于环境的属性放在 application-${env}.properties
文件中,将常用属性放在 application.properties
中.
Place your environment-specific properties in the application-${env}.properties
file and your common properties in application.properties
.
有多种方法可以告诉 spring 使用哪些配置文件,例如 --spring.profiles.active
标志.
There are multiple ways to tell spring which profiles to use, for example the --spring.profiles.active
flag.
见Spring Boot 文档 了解有关属性文件搜索顺序的更多详细信息.
See the Spring Boot documentation for more details about the property file search order.
这篇关于在 SpringBoot 中定义所有环境通用的属性的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!