Home > database >  Is it possible to pass property value to annotation?
Is it possible to pass property value to annotation?

Time:11-20

I want to get this, is it possible?

@Autowired
lateinit var config : AppProperties

@RabbitListener(queues = ["#{\${config.queueName}}"])
fun listen(message: String?) {
    println(message)
}

Could not resolve placeholder 'config.queueName' in value "#{${config.queueName}}"

CodePudding user response:

I think it is not possible in this case, because Spring doesn't know about "config", but you can use SPeL for retrieving property from AppProperties bean, for example:

@RabbitListener(queues = ["#{appProperties.queueName}"])

It should work if your @Autowired AppProperties has bean name = 'appProperties'

I can't check this solution at this moment, but i think that it should work.

UPD:

You can use this soltuion without defining config variable, because SPeL working with beans and defining config variable unnecessarily.

CodePudding user response:

@RabbitListener(queues = ["\${config.queueName}"])

please try like this, you can read about that more at this blog

CodePudding user response:

I hope this value is coming from a properties file. I have not used kotlin but this is how we do in java.

application.yml

config:
  queueName: somequeue

Code:

@RabbitListener(queues = "${config.queueName}")
fun listen(message: String?) {
    println(message)
}
  • Related