Springboot不加载application.dev.properties文件 [英] Springboot not loading application.dev.properties file
问题描述
在我的项目中,我想使用特定于环境的属性文件.例如,如果我将其运行到开发中,则应使用application.dev.properties,在生产中应使用application.prod.properties等.
In my project I want to use environment specific property file. For example if I am running it into development it should use application.dev.properties, for production it should use application.prod.properties and so on.
我的资源文件夹中有下面两个文件.
I have below two files in my resources folder.
- application.properties(用于生产)
- application.dev.properties(用于开发)
每个文件中都有一个如下所示的属性.
I have one properties like below in each file.
对于产品
server.database.host=192.168.1.1
用于开发
server.database.host=192.168.12.125
我有一个像下面这样的课
And I have a class like below
public class DataSource {
@Value(${server.database.host})
String host;
即使我为像--spring.profiles.active=dev
下面是我用来加载dev属性文件的命令.
Below is the command I am using to load the dev properties file.
java -jar myjar.jar --spring.profiles.active=dev
它还会打印活动配置文件是dev,但它始终连接到prod db.
It also prints that active profile is dev but it always connect to prod db.
推荐答案
我注意到的一些问题:
-
@Value
属性名称应为String
,例如@Value("${server.database.host}")
- 特定于配置文件的属性文件应采用
application-{profile}.properties
格式,例如application-dev.properties
- 尝试通过
-D
传递配置文件,例如java -Dspring.profiles.active=dev -jar app.jar
@Value
property name should be aString
like@Value("${server.database.host}")
- Profile specific property files should follow
application-{profile}.properties
format, e.g.application-dev.properties
- Try passing the profile with
-D
likejava -Dspring.profiles.active=dev -jar app.jar
这篇关于Springboot不加载application.dev.properties文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!