Springboot不加载application.dev.properties文件 [英] Springboot not loading application.dev.properties file

查看:1616
本文介绍了Springboot不加载application.dev.properties文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我想使用特定于环境的属性文件.例如,如果我将其运行到开发中,则应使用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.

  1. application.properties(用于生产)
  2. 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.

推荐答案

我注意到的一些问题:

  1. @Value属性名称应为String,例如@Value("${server.database.host}")
  2. 特定于配置文件的属性文件应采用application-{profile}.properties格式,例如application-dev.properties
  3. 尝试通过-D传递配置文件,例如java -Dspring.profiles.active=dev -jar app.jar
  1. @Value property name should be a String like @Value("${server.database.host}")
  2. Profile specific property files should follow application-{profile}.properties format, e.g. application-dev.properties
  3. Try passing the profile with -D like java -Dspring.profiles.active=dev -jar app.jar

这篇关于Springboot不加载application.dev.properties文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆