Is there way to bind a method interceptor to a provider rather than an instance?
e.g. I use the code below to bind interceptors how would I bind INTERCEPTOR to a provider and then to the annotation?
bindInterceptor(
Matchers.any(), Matchers.annotatedWith(ANNOTATION.class), new INTERCEPTOR());
Guice does not allow AOP on instances not built by Guice: Guice AOP Limitations
“Instances must be created by Guice by an @Inject-annotated or no-argument constructor”
This means that instances created with a provider will not be candidates for AOP.
On the flip side, as long as your Provider is instantiated by Guice under the conditions mentioned, your Provider may be a candidate for AOP.
Here’s an example that demonstrates this:
AOP Annotation:
Provider:
Target Example:
Module:
Test Code:
Test Output:
Notice that the “example working…” is not surrounded by the timer code. The Provider.get (“Building…”) is however.
If your question is: can the interceptor (new INTERCEPTOR()) be provided through a Guice Provider, the answer is no. The closest you may get to this functionality is calling the requestInjection() in the module configure method. This will inject your Interceptor with the appropriate code. From your interceptor you may be able to use Providers to avoid any sort of overhead that is causing you slowness during startup.
Here’s what I mean:
Module:
Interceptor:
Hope this answers your question.