在春季加载特定于环境的属性文件 [英] Loading environment specific properties file in spring

查看:111
本文介绍了在春季加载特定于环境的属性文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Spring Boot应用程序,在以下情况下我需要您的帮助.

I am working on a spring-boot application, I need your assistance on below scenario.

我为每个环境都有属性文件,例如application-dev.properties,application-prod.properties等.我的应用程序可以通过spring @Profile批注来加载特定于环境的属性文件吗.

I have properties files for each environment something like application-dev.properties, application-prod.properties etc. Is there way that my application can load environment specific properties file by using spring @Profile annotation.

请帮助.

推荐答案

@Profile不用于加载特定于环境的属性文件.它用于指定bean的配置文件.例如,

@Profile is not for loading environment specific properties file. It is for specifying profile of a bean. For example,

@Profile("dev")
@Component
class Foo {

}

这意味着Foo的bean仅在活动概要文件包含dev时可用.或相反的@Profile("!dev"),这意味着当dev不是活动配置文件时Bean可用.

It means the bean of Foo is only available when the active profiles include dev. Or the opposite @Profile("!dev"), which means the bean is available when dev is not an active profile.

因此,对于加载环境特定的属性文件,由于它是spring-boot,因此您只需指定活动配置文件即可.有几种方法可以指定活动配置文件.

So for loading environment specific properties file, since it is spring-boot, you can just specify the active profiles. There are several ways to specify the active profiles.

  • 环境变量:SPRING_PROFILES_ACTIVE=dev,prod
  • 命令行参数:java -jar app.jar --spring.profiles.active=dev,prod
  • 以编程方式:SpringApplicationBuilder(...).properties("spring.profiles.active=dev,prod).run(...)
  • 默认application.properties或yaml:spring.profiles.active:dev, prod
  • Environment variable: SPRING_PROFILES_ACTIVE=dev,prod
  • command line argument: java -jar app.jar --spring.profiles.active=dev,prod
  • Programmatically : SpringApplicationBuilder(...).properties("spring.profiles.active=dev,prod).run(...)
  • Default application.properties or yaml: spring.profiles.active:dev, prod

这篇关于在春季加载特定于环境的属性文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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