英文:
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
,导致它注入了文字字符串,如日志中所示,而不是环境变量?(顺便说一下,这应该是布尔值 true
或 false
)
<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 = ["app.running-mode.read-write"],havingValue = "true", 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 'MY_API_RUNNING_MODE_READ_WRITE` in value "${MY_API_RUNNING_MODE_READ_WRITE}"
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 = "app.running-mode.read-write",havingValue = "true", matchIfMissing = true)
class MyRestController(private val r : MyRestService){
@GetMapping
// some endpoint here
@PostMapping
// some endpoint here
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论