xiaodududu / google-guice

Automatically exported from code.google.com/p/google-guice
Apache License 2.0
0 stars 0 forks source link

Simplify syntax for injecting into an interceptor #465

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Instead of e.g.:

  Timer timer = new Timer();
  requestInjection(timer); // injects e.g. a Logger
  bindInterceptor(Matchers.any(), Matchers.annotatedWith(Timed.class), timer);

...it would be nice to simply write:

  bindInterceptor(Matchers.any(), Matchers.annotatedWith(Timed.class),
Timer.class);

Original issue reported on code.google.com by eric.j...@gmail.com on 29 Jan 2010 at 8:07

GoogleCodeExporter commented 9 years ago
This might also allow use of constructor injection in the interceptor.

Original comment by eric.j...@gmail.com on 29 Jan 2010 at 8:09

GoogleCodeExporter commented 9 years ago
We intentionally broke constructor injection on interceptors to avoid serious 
and violient 
never-ending recursion pain when an interceptor is itself intercepted.

Original comment by limpbizkit on 30 Jan 2010 at 1:46

GoogleCodeExporter commented 9 years ago
Doesn't non-constructor injection have the same recursion potential? I don't 
suppose
there is a way to implement a requestInjectionWithoutInterception method?

Original comment by eric.j...@gmail.com on 30 Jan 2010 at 6:24

GoogleCodeExporter commented 9 years ago
requestInjection simply sets fields and calls methods annotated with @Inject, 
it does 
not setup interception.

Method interception only works on instances that were created by Guice. 
Interception 
is done by essentially proxy-wrapping the implementation and returning the 
proxy. If 
the instance is created outside Guice then there is no proxy and thus no 
interception.

As limpbizkit mentioned, not allowing interception of interceptor methods is 
safe 
since it eliminates the possibility of infinite interceptor recursion. On the 
downside it 
doesn't allow any Guice-driven interception on interceptors.

Original comment by drew.mcl...@gmail.com on 1 Feb 2010 at 4:21

GoogleCodeExporter commented 9 years ago
Since it appears to be possible to do injection without setting up interception
(using requestInjection), shouldn't it also be possible to do constructor 
injection
without setting up interception, or are the two somehow tied together?

Original comment by eric.j...@gmail.com on 1 Feb 2010 at 6:05