Spring Boot无法在配置文件的profile表达式中读取属性

huangapple go评论92阅读模式
英文:

Spring Boot not able to read properties inside profiles expression

问题

Env Details:

  • Spring Boot 2.1.15.RELEASE
  • Spring 5.2.2.RELEASE

I have application.yml file as below,

creds:
    first: default value
    
---

spring.profiles: dev

creds:
    first: dev value

---

spring.profiles: dev & mobile

creds:
    first: dev-mobile value

and

spring.profiles.active=dev,mobile

However, the problem is that it always loads 'default value'.

Property under profile 'dev & mobile' is never loaded.

Appreciate any help.

Link to Spring Boot Documentation

英文:

Env Details:

  • Spring Boot 2.1.15.RELEASE
  • Spring 5.2.2.RELEASE

I have application.yml file as below,

creds:
	first: default value
    
---

spring.profiles: dev

creds:
	first: dev value


---

spring.profiles: dev & mobile

creds:
	first: dev-mobile value

and

spring.profiles.active=dev,mobile

However, the problem is that it always loads 'default value'.

Property under profile 'dev & mobile' is never loaded.

Appreciate any help.

https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-external-config-multi-profile-yaml

答案1

得分: 0

似乎您将所有环境都添加在同一个文件中
应该是
开发环境一个文件
application-dev.yml

reds:
    first: dev value

生产环境另一个文件

application-production.yml

reds:
    first: production value

依此类推。

英文:

Seems you added every environments in same file
it should be
one file for dev
application-dev.yml

reds:
    first: dev value

another file for production

application-production.yml

reds:
    first: production value

goes on.

答案2

得分: 0

首先,如果您单独定义了个别的配置文件(profile: dev),然后再与其他配置文件组合(再次与dev & mobile组合),则您的application.properties不会选择组合的配置文件(profile: dev & mobile)。这不会导致任何异常,但组合配置文件将不会被选中。

文档说明,我们可以使用符号 &| 以及其他符号来组合配置文件,可以创建任何复杂的组合。但是,我不建议您既单独定义配置文件,又与其他配置文件组合,就像您组合了 dev & mobile 一样,而之前又单独定义了 dev 配置文件。

以下示例展示了它是如何工作的:

application.yaml

creds:
  first: 默认值

---

spring:
  profiles: dev

creds:
  first: dev 值

---

spring:
  profiles: mobile

creds:
  first: 移动值

---

spring:
  profiles: dev & mobile

creds:
  first: dev-mobile 值

如果我们单独定义配置文件并与其他配置文件组合,会发生什么?(就像您所做的那样)。

在此application.yml文件中,我定义了dev、mobile和组合配置文件(dev & mobile),而且如我们所知,还有一个默认配置文件。

如果我不调用任何配置文件,正如我们所知,它将选择默认配置文件。例如,如果我们这样读取属性:

@Value("${creds.first}")
String value;

属性值:默认值

如果 --spring.profiles.active=dev,那么它将选择 dev 配置文件。

属性值:dev 值

如果 --spring.profiles.active=mobile,那么它将选择 mobile 配置文件。

属性值:移动值

如果 --spring.profiles.active=mobile,dev,那么在这里,我们尝试同时调用两个配置文件,理论上我们期望选择组合配置文件。但是它不会被选中。因为我们已经单独定义了配置文件(profile: devprofile: mobile分别),首先它会从mobile配置文件中读取属性,然后从dev配置文件中读取属性,如果找到,然后会覆盖为dev配置文件的属性值。如果没有找到,它将保持为mobile配置文件的属性值。我意思是,它会读取:

属性值:dev 值

如果 --spring.profiles.active=dev,mobile,那么在这里,它首先从dev配置文件中读取属性值,然后再次在mobile配置文件中查找属性,如果找到,它会覆盖该值。如果没有找到,它将保持为dev配置文件的属性值。

这就是为什么它不会读取组合配置文件(dev & mobile)的原因。

它是如何在什么时候读取组合配置文件?

根据文档,如果所提到的配置文件在其他地方没有单独定义,它可以选择组合配置文件:

例如:application.yaml

creds:
  first: 默认值

---

spring:
  profiles: showcase

creds:
  first: 展示值

---

spring:
  profiles: dev & mobile

creds:
  first: dev-mobile 值

在这里,使用 & 将dev和mobile组合在一起,它们没有在其他地方单独定义。因此,如果我们调用 --spring.profiles.active=dev,mobile,它将选择组合配置文件的值。

属性值:dev-mobile 值

最后,如果 --spring.profiles.active=dev--spring.profiles.active=mobile,它将不会选择组合配置文件。因此,它将保持默认配置文件。

属性值:默认值

示例:https://github.com/donthadineshkumar/multi-profile-document.git

英文:

Firstly, there is no way that your application.properties will pick the combined profile (profile: dev & mobile) if you define profiles individually(profile: dev) & also combining it with other profile again(combined with mobile again, I mean dev & mobile). This does not give you any Exceptions, but your combined profile will not get a pick.

The documentation says that, we can combine profiles with symbols & | and other symbols please refer docs, we can make such any complex combinations. But, I don't recommend you define a profile individually & also combine it with other profiles, like you combined dev & mobile, after define dev profile separately.

Following example shows, how it works?

application.yaml

creds:
  first: default value

---

spring:
  profiles: dev

creds:
  first: dev value

---

spring:
  profiles: mobile

creds:
  first: mobile val

---

spring:
  profiles: dev & mobile

creds:
  first: dev-mobile value

What happens, if we define a profile individually & combining it with other profile? (like you did).

In this application.yml file, I have defined dev, mobile & combined profile(dev & mobile), and as we know there is also a default profile.

If I don't invoke any profile, as we know, it will pick default profile. For Example, if we are reading a property like this:

@Value("${creds.first}")
String value;

property value: default value

If --spring.profiles.active=dev , then it will pick dev profile.

property value: dev value

If --spring.profiles.active=mobile , then it will pick mobile profile.

property value: mobile val

If --spring.profiles.active=mobile,dev , then here, we have trying to invoke both profiles at same time, ideally, we expect the combined profile should be picked. But it will be not picked. Because, we have defined separate profiles(profile: dev and profile: mobile separately), firstly, it will read the property from mobile profile and then it reads the property from dev profile, if found, then it overrides with property value from dev profile. If not found, it will stick to property value of mobile profile only. I mean to say, it reads

property value: dev value

If --spring.profiles.active=dev,mobile, then here, it first reads the property value from dev profile, then, the again it looks for the property in mobile profile, if found, overrides the value. If not found, it will stick to property value of dev profile only.

That's why it will not read the combined profile (dev & mobile).

How & when it reads combined profile?

As per documentation, it can pick a combined profile, if the mentioned profiles should not have been defined separately elsewhere:

Example: application.yaml

creds:
  first: default value

---

spring:
  profiles: showcase

creds:
  first: showcase value

---

spring:
  profiles: dev & mobile

creds:
  first: dev-mobile value

here, dev, mobile are combined using & and they are not separately defined elsewhere.
So, now if we invoke --spring.profiles.active=dev,mobile, it will pick the combined profile value

property value: dev-mobile value

Lastly, if --spring.profiles.active=dev or --spring.profiles.active=mobile, it will not pick the combined profile. So, it will sick to default profile.

property value: default value

Sample: https://github.com/donthadineshkumar/multi-profile-document.git

huangapple
  • 本文由 发表于 2020年8月27日 11:22:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/63608685.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定