I ran into this confusion today. Quote from Weld's documentation (right under Section 9.3),
By default, all interceptors are disabled. We need to enable our interceptor. We can do it using beans.xml descriptor of a bean archive. However, this activation only applies to the beans in that archive.
However, in the project that I am currently working on, I have an interceptor for profiling a method. My META-INF/beans.xml
is basically empty:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
version="1.1" bean-discovery-mode="all">
</beans>
Yet I still get expected logs from that profiling interceptor. So, as the title goes, is interceptor really disabled by default?
BTW, I use weld-se
for CDI functionality in the project, since CDI is the only thing I need for the project from the Java EE stack.
update
After messing around interceptors today, I find that if the old @Interceptors
is used to indicate the interception implementation class, you don't need to specify anything in the beans.xml
. However, if you use interceptor binding, i.e., use the @Interceptor
annotation to indicate an interceptor class, you must enable interception by adding the interceptor class to the beans.xml
. According to my experience, this is still true for CDI 1.1, as indicated by the version in the beans.xml
above. BTW, I use org.jboss.weld.se:weld-se:2.0.4.Final
for CDI implementation in this case, which I believe implements CDI 1.1.
@Interceptors
the interceptors are on by default, since you've created a tight coupling between the intercepted object and the interceptor, it will end up enabling it. However, as noted in the docs@Interceptors
is provided by the Interceptor spec, not the EJB or CDI specs, as a result only the rules of the Interceptor spec are expected to be applied (you may find others are as well, but it's not portable). – Honorable@Priority
will enable it, without the inclusion of beans.xml. e.g.@Priority(Interceptor.Priority.APPLICATION)
– Kronos