xiaodududu / google-guice

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

RFE: Ability to delegate injection for unknown types #778

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Use-case: Jersey 2.0 depends on HK2 (an implementation of JSR330). In order to 
integrate Guice into Jersey 2.0 I am building a Guice-HK2 bridge whereby HK2 
delegate to Guice for unknown types and Guice delegates to HK2 for unknown 
types (with a mechanism to prevent endless loops).

HK2 already has a mechanism to defer injection to Guice, but Guice does not 
provide a mechanism to defer injection to HK2. I tried using TypeListener and 
asking for help on the mailing list but there doesn't seem to be a way to 
implement this as of Guice 3.0.

When trying to inject the following:

@javax.inject.Inject
public DepartmentsResource(com.google.inject.Injector injector, 
javax.ws.rs.core.UriInfo uriInfo)

Guice understands @Inject, Injector but not UriInfo. It never invokes the 
TypeListener for DepartmentsResource or UriInfo, so I don't have the chance to 
register a MembersInjector to delegate injection to HK2.

Original issue reported on code.google.com by cow...@bbs.darktech.org on 28 Oct 2013 at 10:11

GoogleCodeExporter commented 9 years ago
FWIW this is my old BindingFactory patch from issue 49 - it still needs 
updating to apply cleanly against master, but may be useful as a starting point 
for discussion.

Original comment by mccu...@gmail.com on 28 Oct 2013 at 11:11

Attachments:

GoogleCodeExporter commented 9 years ago
How is this different than issue 49?

Original comment by sberlin on 28 Oct 2013 at 3:25

GoogleCodeExporter commented 9 years ago
Not sure, though maybe the use-case is different. Issue 49 was a bit vague to 
begin with. I have provided a concrete use-case, with strong demand [1], with 
no known workaround.

[1] See the number of votes on https://java.net/jira/browse/JERSEY-1950 (this 
is a large number by Jersey standards).

Original comment by cow...@bbs.darktech.org on 28 Oct 2013 at 3:37

GoogleCodeExporter commented 9 years ago
Another approach (which doesn't seem to be covered by issue 49) is to extend 
TypeListener to cover constructor injection. If TypeListener were fired for 
DepartmentsResource I could register the equivalent of MemberInjector for the 
constructor parameters.

Original comment by cow...@bbs.darktech.org on 28 Oct 2013 at 7:38

GoogleCodeExporter commented 9 years ago
Sam,

Who do we need to bring on board to move this issue forward?

Original comment by cow...@bbs.darktech.org on 31 Oct 2013 at 9:34

GoogleCodeExporter commented 9 years ago
I'm not sure.  Is there a reason that Stuart's suggested flow (use a module 
that takes other modules and uses the SPI to scan them & write binding 
statements for the remaining injection points) won't work for you?

Guice very strongly leans against allowing this kind of autobinding.

Original comment by sberlin on 31 Oct 2013 at 11:01

GoogleCodeExporter commented 9 years ago
I guess I didn't fully understand what he meant. Did he mean I should inject a 
Guice Module that takes HK2 DynamicConfiguration as input and convert 
DynamicConfiguration bindings into Module bindings?

Guice seems to be geared towards binding a configuration at init time, whereas 
HK2 allows bindings to change at runtime. (John Wells, please correct me if I'm 
wrong.)

It's not clear to me how I'd add/remove/modify bindings at runtime with Guice, 
even if I could register an event listener for HK2's DynamicConfiguration.

Injector.createChildInjector() allows me to add new bindings at runtime, but 
it's not clear how to change/remove them at a later time.

I've asked John to provide us with some background information about HK2 
because I'm not very familiar with how it work.

Original comment by cow...@bbs.darktech.org on 1 Nov 2013 at 12:21

GoogleCodeExporter commented 9 years ago
HK2 does indeed allow for dynamic additions and removals of underlying
descriptors at any time (given the proper permissions).  This ends up being
useful in dynamic systems such as OSGi systems or really any module system
(jigsaw etc).  I guess more specific questions would get more specific
answers lol.  More information can be found here of course:
https://hk2.java.net/

Original comment by jwells13...@gmail.com on 3 Dec 2013 at 7:59