David Stowell
David Stowell

Reputation: 129

Multiple method interceptions in Guice

I am working with Guice's method interception feature. What I need to know is how to properly implement multiple interceptors, of the form:

this.bindInterceptor(Matchers.any(), Matchers.any(), new Interceptor1(), new Interceptor2());

Specifically, if there is an invocation of proceed() in both interceptors, what happens? Does the intercepted method get called twice? Or does the proceed() in the first interceptor invoke the second interceptor, which then invokes the method? Or should only one interceptor have a proceed()?

Thanks

Upvotes: 2

Views: 1245

Answers (1)

condit
condit

Reputation: 10972

Both interceptors can (and should) call proceed. This way they can be used as independent aspects (i.e. transactions and logging). In fact if you don't call proceed from your outer interceptor then the next interceptor will not fire.

The method interceptors will be called in a stack-like fashion based on the order of the bindInterceptor calls. In your example it would look like this:

Interceptor1 entry
Interceptor1 proceed
  Interceptor2 entry
  Interceptor2 proceed
    Method
  Interceptor2 exit
Interceptor1 exit

Upvotes: 3

Related Questions