简体   繁体   中英

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

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:

@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)

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM