Spring Boot中的”@ConditionalOnProperty(value =)”注解用于注入字面字符串。

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

Spring boot "@ConditionalOnProperty(value =)" injecting literal String

问题

@RestController
@ConditionalOnProperty(value = ["app.running-mode.read-write"], havingValue = "true", matchIfMissing = true)
class MyRestController(private val r: MyRestService) {
    @GetMapping
    // some endpoint here

    @PostMapping
    // some endpoint here
}
app.running-mode.read-write=${MY_API_RUNNING_MODE_READ_WRITE}

当我运行应用程序时,有时会崩溃,出现 java.lang.IllegalStateException,并且我看到错误信息是:

在值中无法解析占位符 'MY_API_RUNNING_MODE_READ_WRITE`,该值为 "${MY_API_RUNNING_MODE_READ_WRITE}"

这应该是 Azure 设置的环境变量,但我可能是不是错误地定义了我的 @ConditionalOnProperty,导致它注入了文字字符串,如日志中所示,而不是环境变量?(顺便说一下,这应该是布尔值 truefalse




<details>
<summary>英文:</summary>

I have a Spring Boot application, with a class annotated as a `@RestController` and also having `@ConditionalOnProperty` annotation on my class `MyRestController.kt` (kotlin) as below:

```kotlin
@RestController
@ConditionalOnProperty(value = [&quot;app.running-mode.read-write&quot;],havingValue = &quot;true&quot;, matchIfMissing = true)

class MyRestController(private val r : MyRestService){
    @GetMapping
    // some endpoint here
    
    @PostMapping
    // some endpoint here
}

My application.properties looks like this:

app.running-mode.read-write=${MY_API_RUNNING_MODE_READ_WRITE}

When I run my app sometimes, it crashes for a java.lang.IllegalStateException on my class that contains this ConditionalOnProperty annotation MyRestController.kt, and I see it says:

Could not resolve placeholder &#39;MY_API_RUNNING_MODE_READ_WRITE` in value &quot;${MY_API_RUNNING_MODE_READ_WRITE}&quot;

This should be an environment variable set by Azure, but do I possibly have my @ConditionalOnProperty defined incorrectly such that it is injecting the literal String, as seen in the logs, instead of the environment variable? (Which should be a boolean true or false, by the way)

答案1

得分: 1

问题可能出在您提供值的方式上。由于您正在从环境中为不同的环境注入值,所以该值可能不存在,甚至在环境中提供错误。请检查以确保它被正确提供。确保环境配置了键 "MY_API_RUNNING_MODE_READ_WRITE"。

既然您是从 application.properties 中读取值,那么可以尝试在删除 '[]' 后按以下方式提供:

@RestController
@ConditionalOnProperty(value = "app.running-mode.read-write", havingValue = "true", matchIfMissing = true)

class MyRestController(private val r: MyRestService) {
    @GetMapping
    // 这里是某个端点

    @PostMapping
    // 这里是某个端点
}
英文:

The issue could arise from how you have provided the value. Since you are injecting the value from environment for the different env, it's possible that the value may not be present or even provided wrongly in the environment. Check to veirfy that it's provided correctly. Make sure that the environment is configured with the key "MY_API_RUNNING_MODE_READ_WRITE".

Since you are reading the value from application.properties then try providing it like below after removing '[]' :

@RestController
@ConditionalOnProperty(value = &quot;app.running-mode.read-write&quot;,havingValue = &quot;true&quot;, matchIfMissing = true)

class MyRestController(private val r : MyRestService){
    @GetMapping
    // some endpoint here
    
    @PostMapping
    // some endpoint here
}

huangapple
  • 本文由 发表于 2020年9月28日 14:00:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/64096705.html
匿名

发表评论

匿名网友

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

确定