[英]Add CDI decorator to Message Driven Bean
Given the following Message Driven Bean, is it possible to define a global decorator from the CDI specification to add additional behaviour?给定以下消息驱动 Bean,是否可以根据 CDI 规范定义全局装饰器以添加其他行为?
@MessageDriven
public class MyMessageDrivenBean implements MessageListener {
@Override
public void onMessage(Message m) {
}
}
The decorator looks like this:装饰器看起来像这样:
@Decorator
@Priority(Interceptor.Priority.APPLICATION)
public abstract DecorateMyMessageDrivenBean implements MessageListener {
@Inject
@Delegate
@Any
private MessageListener delegate;
@Override
public void onMessage(Message m) {
}
}
Currently, the decorator is not being executed.目前,装饰器没有被执行。 I have added a beans.xml file to my module.我在我的模块中添加了一个 beans.xml 文件。
The short answer (but don't lose hope) is no, because a @MessageDrivenBean
is not a CDI managed bean, and only CDI managed beans can be decorators .简短的回答(但不要失去希望)是否定的,因为@MessageDrivenBean
不是 CDI 托管 bean, 只有 CDI 托管 bean 可以是装饰器。
Now, what you might be able to do (and I don't have experience doing this myself) is something like this:现在,你可能能够做的(我自己没有这样做的经验)是这样的:
@javax.annotation.Resource
to call for a MessageListener
to be injected into a field by Java EE, not by CDI (so it will be looked up in JNDI)使用@javax.annotation.Resource
调用MessageListener
由 Java EE 注入字段,而不是由 CDI(因此它将在 JNDI 中查找)@Produces
MessageListener
instances by using a combination of that field's contents and the createInterceptionFactory
method通过使用该字段的内容和createInterceptionFactory
方法的组合,编写一个@Produces
MessageListener
实例的生产者方法@Inject
the MessageListener
so produced wherever you want to use it @Inject
如此产生的MessageListener
,无论你想在哪里使用它An InterceptionFactory
is really the only mechanism you have to dynamically add interception (and a decorator is just a very special case of interception) to Things That Are Not CDI Managed Beans. InterceptionFactory
确实是您必须向非 CDI 托管 Bean 的事物动态添加拦截(装饰器只是拦截的一种非常特殊情况)的唯一机制。
Finally, this will only work if you use CDI 2.0 (which is in Java EE 8 or higher).最后,这仅在您使用 CDI 2.0(在 Java EE 8 或更高版本中)时才有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.