【问题标题】:Is it possible to pass property value to annotation?是否可以将属性值传递给注释?
【发布时间】:2021-12-30 18:19:29
【问题描述】:

我想要这个,可以吗?

@Autowired
lateinit var config : AppProperties

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

无法解析值“#{${config.queueName}}”中的占位符“config.queueName”

【问题讨论】:

    标签: spring-boot kotlin


    【解决方案1】:

    我认为在这种情况下是不可能的,因为 Spring 不知道“config”,但是您可以使用 SPeL 从 AppProperties bean 中检索属性,例如:

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

    如果您的 @Autowired AppProperties 的 bean name = 'appProperties',它应该可以工作

    我目前无法检查此解决方案,但我认为它应该可以工作。

    UPD:

    您可以在不定义 config 变量的情况下使用此解决方案,因为 SPeL 使用 bean 并不必要地定义 config 变量。

    【讨论】:

      【解决方案2】:
      @RabbitListener(queues = ["\${config.queueName}"])
      

      请尝试这样,您可以在blog了解更多信息

      【讨论】:

        【解决方案3】:

        我希望这个值来自属性文件。我没有使用过 kotlin,但这就是我们在 java 中的做法。

        application.yml

        config:
          queueName: somequeue
        

        代码:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-02-17
          • 2022-06-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-07-26
          • 2016-10-10
          • 1970-01-01
          相关资源
          最近更新 更多