I am just wondering whether we can create a meta-annotation and reuse it everywhere. For example, I have an annotation like so:
@QueueBinding(value = @Queue, exchange = @Exchange(value = "MY_FANOUT_EXCHANGE", type = ExchangeTypes.FANOUT))
I need to use it in multiple places. I just want to simplify it something like:
@MyFanoutExchangeBinding = @QueueBinding(value = @Queue, exchange = @Exchange(value = "MY_FANOUT_EXCHANGE", type = ExchangeTypes.FANOUT))
and then use the @MyFanoutExchangeBinding
on methods everywhere. If I understand annotations correctly, its not possible. Or is it?
答案 0 :(得分:1)
元注释在此上下文中不起作用,因为@RabbitListener
bindings
属性是QueueBinding[]
且注释没有继承。
要实现它需要一个自定义的RabbitListenerAnnotationBeanPostProcessor
,但由于它不能以通用的方式完成,我认为它不能成为框架的一部分,至少在你描述的方式。
我们可能会以其他方式减少样板;随意打开JIRA issue,我们可以看一看可能的情况。
修改强>
收到JIRA Issue之后我又考虑了一些问题,您可以使用自定义侦听器注释获得所需结果,并使用@RabbitListener
进行元注释...
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@RabbitListener(bindings = @QueueBinding(
value = @Queue,
exchange = @Exchange(value = "test.metaFanout", type = ExchangeTypes.FANOUT, autoDelete = "true")))
public @interface MyAnonFanoutListener {
}
public static class MetaListener {
@MyAnonFanoutListener
public void handle1(String foo) {
...
}
@MyAnonFanoutListener
public void handle2(String foo) {
...
}
}
每个侦听器都会获得一个绑定到扇出交换的匿名自动删除队列。