简体   繁体   English

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

[英]Spring Boot not able to read properties inside profiles expression

Env Details:环境详情:

  • Spring Boot 2.1.15.RELEASE Spring Boot 2.1.15.RELEASE
  • Spring 5.2.2.RELEASE春季 5.2.2. 发布

I have application.yml file as below,我有如下的 application.yml 文件,

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 spring.profiles.active=开发,移动

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 https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-external-config-multi-profile-yaml

Seems you added every environments in same file it should be one file for dev application-dev.yml似乎您在同一个文件中添加了每个环境,它应该是 dev application-dev.yml 的一个文件

reds:
    first: dev value

another file for production另一个用于生产的文件

application-production.yml应用程序生产.yml

reds:
    first: production value

goes on.继续。

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 ) .首先,如果您单独定义配置文件(配置文件:dev)并再次将其与其他配置文件组合(再次与手机组合,我的意思是开发),您的application.properties将无法选择组合配置文件和移动 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.但是,我不建议您单独定义一个配置文件并将其与其他配置文件结合起来,就像您将dev 和 mobile结合起来一样,在单独定义dev配置文件之后。

Following example shows, how it works?下面的例子显示,它是如何工作的?

application.yaml应用程序.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.在这个 application.yml 文件中,我定义了 dev、mobile 和组合配置文件(dev 和 mobile),而且我们知道还有一个默认配置文件。

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.如果--spring.profiles.active=dev ,那么它将选择开发配置文件。

property value : dev value属性值:开发值

If - -spring.profiles.active=mobile , then it will pick mobile profile.如果 - -spring.profiles.active=mobile ,那么它将选择移动配置文件。

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.如果--spring.profiles.active=mobile,dev ,那么在这里,我们尝试同时调用两个配置文件,理想情况下,我们希望选择组合配置文件。 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.因为,我们定义了单独的配置文件(配置文件:dev配置文件:mobile分开),首先,它将从移动配置文件中读取属性,然后从开发配置文件中读取属性,如果找到,则使用开发配置文件中的属性值覆盖. 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.如果--spring.profiles.active=dev,mobile ,那么在这里,它首先从开发配置文件中读取属性值,然后再次在移动配置文件中查找属性,如果找到,则覆盖该值。 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示例: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.在这里,dev、mobile 使用 & 组合在一起,它们在别处没有单独定义。 So, now if we invoke --spring.profiles.active=dev,mobile , it will pick the combined profile value所以,现在如果我们调用--spring.profiles.active=dev,mobile ,它将选择组合的配置文件值

property value : dev-mobile value属性值:开发移动值

Lastly, if --spring.profiles.active=dev or --spring.profiles.active=mobile , it will not pick the combined profile.最后,如果--spring.profiles.active=dev--spring.profiles.active=mobile ,它不会选择组合配置文件。 So, it will sick to default profile.因此,默认配置文件会很糟糕。

property value : default value属性值:默认值

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Spring Boot 中使用 spring 配置文件读取多个属性文件 - Read Multiple Properties File using spring profiles in Spring Boot 具有外部属性的Spring Boot配置文件 - Spring boot profiles with external properties 无法从 JUnit 中的 Spring Boot 读取应用程序属性? - Not able to read application properties from Spring Boot in JUnit? 如何在 spring 引导中读取构造函数内的 application.properties 值? - How to read application.properties value inside the constructor in spring boot? Spring 引导读取应用程序属性中的环境变量 - Spring Boot read environment variables inside application-properties 未读取外部 Spring 引导属性 - External Spring boot properties not read Spring 引导读取属性到 Map - Spring boot read properties into Map 无法从 API 响应(Spring Boot Service)读取 Angular 中的 cookies(正在读取)- 无法读取 Z37A6259CC0C1DAE299A 的属性(正在读取) - Not able to read cookies in Angular from API response (Spring Boot Service) - Cannot read properties of null (reading 'headers') 使用application.properties文件激活配置文件以进行弹簧启动测试 - Activate profiles for spring boot test using application.properties file Spring Boot Pom-如何从聚合Pom继承配置文件和属性? - Spring Boot pom - how to inherit profiles and properties from aggregator pom?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM