我有 spring 应用程序的 application.properties 文件,其中包含一些简单的属性:
queue=my.test.q
在java代码中我需要将队列指定为@RabbitListener:
@Component
public class Handler {
@RabbitListener(queues = "my.test.q")
public void handleMessage(Message message) {
...
}
这可行,但我想将参数传递给注释,我尝试了以下方法,但没有一个起作用。
@Component
public class Handler {
@Value("${queue}")
private String queueName;
@RabbitListener(queues = @Value("${queue}") <-- not working
@RabbitListener(queues = queueName)) <--- not working
public void handleMessage(Message message) {
...
}
有可能吗?
正如你在@RabbitListener 注释的 javadoc http://docs.spring.io/autorepo/docs/spring-amqp-dist/1.4.5.BUILD-SNAPSHOT/api/org/springframework/amqp/rabbit/annotation/RabbitListener.html#queues--,queues 属性是一个字符串表,因此您不能为其分配注释。在 Java 中,您根本无法将变量分配给注释属性,因为它们需要编译常量 https://stackoverflow.com/questions/2065937/how-to-supply-value-to-an-annotation-from-a-constant-java.
我现在无法测试它,但 javadoc 似乎建议这应该有效(注意,它说它可能返回 SpEL 表达式):
@RabbitListener(queues = "${queue}")
public void handleMessage(Message message) {
...
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)