Closed pabloromanelli closed 9 years ago
This is confusing, but by design. You're binding the interceptor in the child, so it only applies to types in the child injector. By default, Guice promotes dependencies to the parent-most-injector that can house them -- so if you're using just-in-time bindings (e.g, you didn't explicitly bind the type in the child injector's module) and the type doesn't have dependencies that can only be satisfied in the child, then Guice will create the binding in the parent injector. This is mostly necessary for correctness, otherwise programs become non-deterministic depending on the timing of instantiation. (Imagine if something in the parent also wanted the type -- if it was created in the child first, then the parent's use of it would fail.)
To avoid this, bind your type in the child injector's module. You can also use requireExplicitBindings to prevent all just-in-time bindings, which prevents this class of errors.
Very clear explanation. Thank you @sameb !
I can intercept JIT bindings over the root injector, but when trying to intercept a JIT binding configured in a child injector, it doesn't intercept the methods.
Apparently it works if the class has a dependency registered in the child injector (maybe it's related to #888).
I have some tests to reproduce the issue. Tested with guice 3 and guice 4.0-SNAPSHOT (guice-4.0-20141205.235717-17.jar):
https://gist.github.com/pabloromanelli/0b14c31ecb20d19139d2