4

I have a question. Whenever we have EJB jars

<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>

Now my MonitoringInterceptor Intercept all EJBs. Here comes a question: can I do something similar with CDI Interceptors?

<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>

I would like to have MonitoringInterceptor set by default to all CDI Beans injected to application. How can I acquire that?

Svetlin Zarev
  • 14,713
  • 4
  • 53
  • 82
ArturSkowronski
  • 1,722
  • 13
  • 17
  • 4
    I don't think there is a declarative way to do it - with good reason as "all beans" is a rather vague concept in the world of CDI. Beans come and go even after deployment time. This article describes a way to do it programmatically for all beans which are registered with the manager, which will possibly net you your desired result: http://www.byteslounge.com/tutorials/java-ee-add-cdi-interceptor-programmatically . Untested and nothing of my own in here, so I'll have to leave it as a comment. – Gimby Jun 17 '15 at 17:30
  • Perfect soultion. Thank you! – ArturSkowronski Jun 18 '15 at 08:43

2 Answers2

0

A way would be to create an Extension that adds the Annotation to the classes.

As practical example you can look at EjbExtensionExtended

processAnnotatedType(@Observes ProcessAnnotatedType<T> pat) 

makes sure that the beans can be handled

createEJBWrapper 

adds @EjbTransactional to the class if necessary.

aschoerk
  • 3,333
  • 2
  • 15
  • 29
0

I don't think there is a declarative way to do it - with good reason as "all beans" is a rather vague concept in the world of CDI. Beans come and go even after deployment time. This article describes a way to do it programmatically for all beans which are registered with the manager, which will possibly net you your desired result: byteslounge.com/tutorials/…

aschoerk
  • 3,333
  • 2
  • 15
  • 29
  • I posted the comment as answer, to avoid it to be listed as unanswered – aschoerk Aug 26 '17 at 05:05
  • I found this article two years ago and resolved my problem :) I think it's valid answer – ArturSkowronski Oct 11 '17 at 07:30
  • (I know this is old; just wanted to correct something.) Beans in CDI do not "come and go". A bean is a factory of contextual instances. CDI beans are immutable after the container starts up. That is one of CDI's strengths. – Laird Nelson Jun 25 '20 at 20:29