【问题标题】:Spring custom parameter in application.ymlapplication.yml 中的 Spring 自定义参数
【发布时间】:2018-04-11 09:16:43
【问题描述】:

我有一个 spring-boot 应用程序,它在 src/main/resources 中有一个 application.yml 文件。
我需要为每个弹簧配置文件添加我的自定义参数,如下所示:

spring:
   profiles: dev
   myCustomParameter: value

但myCustomParameter 被突出显示并显示消息
Unknown property 'spring.myCustomParameter'

我该如何克服这个问题?

【问题讨论】:

  • 别担心。 Intellij 有时在识别某些属性时会遇到一些问题。它不会影响您的代码执行

标签: spring yaml


【解决方案1】:

IDE 将像这样工作:假设您有 User 对象,并且它与 Profile 对象关联,并且 Profile 具有 firstName 和 lastName 属性。

那么在yaml中user的profile的细节会喜欢

user:
   profile:
       firstName: "Jhon"
       firstName: "DOE"

在您的情况下也是如此:profiles 是 spring bean 的一部分,但 myCustomParameter 是未知的。

在您的情况下,如果您需要访问myCustomParameter 值,那么您需要

@Value("${spring.myCustomParameter})
private String myCustomParameter;

但如果你想直接访问myCustomParameter,你需要这样定义:

 spring:
   profiles: dev

 myCustomParameter: value

那么就可以直接访问了:

@Value("${myCustomParameter})
private String myCustomParameter;

【讨论】:

    猜你喜欢
    • 2022-11-22
    • 2018-06-21
    • 1970-01-01
    • 2023-02-14
    • 2021-09-25
    • 2021-07-20
    • 2020-03-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多