繁体   English   中英

CDI Bean 中的默认拦截器

[英]Default Interceptors in CDI Beans

我有个问题。 每当我们有 EJB jar 时

<ejb-jar
   xmlns = "http://java.sun.com/xml/ns/javaee"
   version = "3.0"
   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd"
        >
    <interceptors>
        <interceptor>
            <interceptor-class>net.bull.javamelody.MonitoringInterceptor</interceptor-class>
        </interceptor>
    </interceptors>
    <assembly-descriptor>
        <interceptor-binding>
            <ejb-name>*</ejb-name>
            <interceptor-class>net.bull.javamelody.MonitoringInterceptor</interceptor-class>
        </interceptor-binding>
    </assembly-descriptor>
</ejb-jar>

现在我的MonitoringInterceptor拦截所有 EJB。 这里有一个问题:我可以用 CDI 拦截器做类似的事情吗?

<beans xmlns="http://java.sun.com/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
      http://java.sun.com/xml/ns/javaee
      http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
    <interceptors>
        <class>net.bull.javamelody.MonitoringInterceptor</class>
    </interceptors>

</beans>

我希望将MonitoringInterceptor默认设置为注入到应用程序的所有 CDI Bean。 我怎样才能获得它?

一种方法是创建一个将注释添加到类的扩展。

作为实际示例,您可以查看EjbExtensionExtended

processAnnotatedType(@Observes ProcessAnnotatedType<T> pat) 

确保可以处理豆子

createEJBWrapper 

如有必要,将 @EjbTransactional 添加到类中。

我不认为有一种声明式的方法可以做到这一点 - 有充分的理由,因为“所有豆子”在 CDI 世界中是一个相当模糊的概念。 即使在部署时间之后,Bean 也会来来去去。 本文描述了一种以编程方式为管理器注册的所有 bean 执行此操作的方法,这可能会为您带来所需的结果: byteslounge.com/tutorials/...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM