Seems there are two ways to binding interceptor to target class/method:
interceptor binding type
(aka, a custom annotation annotated with @InterceptorBinding itself, for example @Logged), and using it on target class/methodI am using interceptor in CDI environment. My question is, does it is completely unnecessary to declare a extra interceptor binding type
if I using @Interceptors to binding interceptor to my target methods?
If answer is yes, then why IntelliJ IDEA constantly complaint me a error
@Interceptor must specify at least one interceptor binding
when I am not annotating interceptor binding type
on my interceptor?
If answer is no, I already binding my interceptor to target class/method with @Interceptors(arrayOfMyInceptor) directly, why declare a extra interceptor binding type
and using it on my interceptor?
I search the web but cannt found anything about difference of this two approaches, hope SO can solve my problem.
Thank you for your patience.
The annotations @Interceptor
and other costum annotations like @Logged
are supposed to be invoked on a interceptor class, e.g.
@Logged
@Interceptor
@Priority(Interceptor.Priority.APPLICATION)
public class LoggedInterceptor implements Serializable { ... }
The annotation @InterceptorBinding
has to be invoked on the annotation you want to create to make clear it's somewhat of a "interceptor qualifier".
@Inherited
@InterceptorBinding
@Retention(RUNTIME)
@Target({METHOD, TYPE})
public @interface Logged {
}
Then you can invoke the interceptor-binding annotation on a (managed) bean or its methods.
@Logged
public String pay() {...}
@Logged
public void reset() {...}
See the java tutorial for more help https://docs.oracle.com/javaee/7/tutorial/cdi-adv006.htm
EDIT
Because I misread your question, here's my edit:
The annotation @Interceptors
is like a collection of interceptors.
By passing several interceptor classes (e.g. LoggedInterceptor
from @Logged
) to the value
variable of the applied @Interceptors
annotation all those inteceptor bindings are invoked:
@Interceptors({LoggedInterceptor.class,
OtherInterceptor.class,.....})
Thus you need at least one interceptor binding for @Interceptors
So you need an interceptor binding for the interceptor class itself but not for the target class since it's already mentioned in the @Interceptors annotation.
See the API documentation https://docs.oracle.com/javaee/7/api/javax/interceptor/Interceptors.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With