xiaodududu / google-guice

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

InjectionListener.afterInjection() not called for instances created via Providers #565

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
If I register an afterInjection listener, I expect to get notified of instances 
created via Providers. The listener is called for the ProviderMethod, but not 
the created instance. 

See enclosed test. Comment out the provider and listener gets called for the 
Foo creation. With the Provider, however, it doesn't.

Original issue reported on code.google.com by Randg...@gmail.com on 28 Oct 2010 at 8:44

Attachments:

GoogleCodeExporter commented 9 years ago
The reason you're not being notified of injection is because Foo isn't being 
injected.  The provider is manually constructing it and returning it, and no 
@Inject fields in it are injected.  You can verify this by adding an @Inject 
String data within Foo and printing it out, it will print out 'null' for data.

But.. if you inject a MembersInjector<Foo> into your @Provides method, and call 
membersInjector.injectMembers(foo) before returning it, then it *will* be 
injected (it will print out a blank string for data instead of null), and 
afterInjection *will* be notified.

So, if there's any bug here, it would be that instances returned by provider 
methods aren't injected... but I'm not exactly sure that's a bug.

Original comment by sberlin on 29 Oct 2010 at 11:48

GoogleCodeExporter commented 9 years ago
Guice is creating the instance by calling the Provider method. I'd like a way 
to listen for this happening. How can I do this?

Original comment by Randg...@gmail.com on 29 Oct 2010 at 5:04