0
votes

I want to get value from application.yml, but I got "This annotation is not applicable to target 'local variable" for this part,how to solve this problem?

@Value("\${aws.secretsManager.secretName}")
val secretName: String? = ""

@Configuration
@EnableConfigurationProperties
@ConfigurationProperties

fun getSecret() {

        @Value("\${aws.secretsManager.secretName}")
        val secretName: String? = ""


        val region = "us-west-2"
        val logger: Logger = LoggerFactory.getLogger(GetSecretConfig::class.java)

        // Create a Secrets Manager client
        val client = AWSSecretsManagerClientBuilder.standard().withRegion(region).build()
        val getSecretValueRequest = GetSecretValueRequest().withSecretId(secretName)

        var getSecretValueResult: GetSecretValueResult? = try {
            client.getSecretValue(getSecretValueRequest)
        } 
}

application.yml

aws:
  secretsManager:
    secretName: "test-mvp"
    region: "us-west-2"
    user: "root"
    password: "root"
1

1 Answers

0
votes

From the @Value javadoc:

Annotation used at the field or method/constructor parameter level that indicates a default value expression for the annotated element.

The @Value annotation is defined as follow:

@Target(value = {FIELD, METHOD, PARAMETER, ANNOTATION_TYPE})
@Retention(value = RUNTIME)
@Documented
public @interface Value

As you see by the @Target, the @Value annotation it's not intended to be used in a LOCAL_VARIABLE.

The solution is to define the secretName variable outside of the function - as a field of the class.