vipx / google-guice

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

Add support for injection into non-static inner classes #749

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Description of the issue:
New feature. Support injection into inner classes that are not static.

Using guice-3.0.

When I last used Jboss 4.x w/ EJB 3.0 (like 6-7 year old technology) - it's 
@EJB injection supported injection into (non-static) inner classes. I also 
believe that more modern CDI via javax.inject.Inject - @Inject also supports 
injection into (non-static) inner classes. It would be nice if Guice supported 
this.

By not supporting this, it requires class refactoring / restructuring into more 
complex forms for the sole purpose of making Guice injection work which is kind 
of crazy.

Steps to reproduce:
1. Create a class which holds a non-static inner class which uses guice @Inject 
(com.google.inject.Inject) to try to inject a resource. 
2. Execute the code
3. Receive error message:

Caused by: com.google.inject.ConfigurationException: Guice configuration errors:

1) Injecting into inner classes is not supported.  Please use a 'static' class 
(top-level or nested) instead of <some.package.OutterClassName$InnerClassName>

Original issue reported on code.google.com by FireW...@gmail.com on 17 May 2013 at 6:21

GoogleCodeExporter commented 9 years ago
Hrmm I don't see how to edit this. Close the issue, i was misunderstanding what 
the problem was. Thought it was because i was using injection in my inner class 
but i was trying to inject my inner class (so I could use @Transactional). I'm 
not sure if that is supported by other injection frameworks or not.

Original comment by FireW...@gmail.com on 17 May 2013 at 7:36

GoogleCodeExporter commented 9 years ago
Closing as requested.

PS. I think Spring might support injection of non-static inner classes, by 
relying on the fact that the inner class has an implicit constructor that 
accepts a reference to the outer instance. However, you can only create the 
inner instance inside the context of an outer instance (ie. when configuring 
with nested XML) and there are gotchas: 
http://stackoverflow.com/questions/15783854/how-to-define-a-bean-for-an-non-stat
ic-inner-class-in-spring-avoiding-circula

Original comment by mccu...@gmail.com on 17 May 2013 at 8:31