Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How create CDI interceptor as singleton?

The code

@Interceptor(SomeInterceptor.class)
public class SomeClass {
    @Interceptor(OtherInterceptor.class)
    public Object method1 (Object param) {
        return param;
    }


    public Object method2 (Object param) {
        return param;
    }
}

The question 1

Are SomeInterceptor and OtherInterceptor created in each method call or not?

The question 2

Is it possible to "setup" Inteceptor class to make it singleton? E.g. SomeInterceptor and OtherInterceptor are created only once.

like image 460
Cherry Avatar asked Sep 20 '26 10:09

Cherry


1 Answers

According to CDI specification there should be at most one interceptor per CDI bean it intercepts and its lifecycle is tied to the one of the bean.
So if your bean is @ApplicationScoped there will be only one 'singletone' interceptor.
Also there was this bug in Weld.

like image 91
Yuri Avatar answered Sep 22 '26 23:09

Yuri