Discussion:
Inject but pass an existing reference?
Robert Nicholson
2017-07-14 15:39:24 UTC
Permalink
So I create an instance of a class using

injector.getInstance(Someclass.class);

this returns me an instance of Someclass

what I want is at the point of the injection for Someclass to have a
reference to "this" ie. the instance where I'm performing
the injection.

I don't want Guice to create a new object as this object already exists.

public Class A {
public createSomeclass() {
SomeModule module = new Module
return injector.getInstance(Someclass.class)
}

}

So I want the instance of Someclass to have a reference of the instance of
Class A where the inject is taking place.

So how do I do this without needing to write
someClassInstance.setClassAReference(this)

Currently Guice is not responsible for instantiating the instance of class
A.
--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice+***@googlegroups.com.
To post to this group, send email to google-***@googlegroups.com.
Visit this group at https://groups.google.com/group/google-guice.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/c123d9ee-78df-4c68-b13c-41e2c83e05e7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Stephan Classen
2017-07-14 21:12:58 UTC
Permalink
I don't think guice can do this. Maybe there is a hook you could use to take controle while the injection happens. Does someone else know more?

What you could do as an alternative is to use assisted injection and then manually pass this as an assisted parameter. The advantages over calling a setter are:
- this is already passed to the constructor.
- because it is part of the interface one cannot forget to pass it

Hope this helps.
Post by Robert Nicholson
So I create an instance of a class using
injector.getInstance(Someclass.class);
this returns me an instance of Someclass
what I want is at the point of the injection for Someclass to have a
reference to "this" ie. the instance where I'm performing
the injection.
I don't want Guice to create a new object as this object already exists.
public Class A {
public createSomeclass() {
SomeModule module = new Module
return injector.getInstance(Someclass.class)
}
}
So I want the instance of Someclass to have a reference of the instance of
Class A where the inject is taking place.
So how do I do this without needing to write
someClassInstance.setClassAReference(this)
Currently Guice is not responsible for instantiating the instance of class
A.
--
You received this message because you are subscribed to the Google
Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send
Visit this group at https://groups.google.com/group/google-guice.
To view this discussion on the web visit
https://groups.google.com/d/msgid/google-guice/c123d9ee-78df-4c68-b13c-41e2c83e05e7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice+***@googlegroups.com.
To post to this group, send email to google-***@googlegroups.com.
Visit this group at https://groups.google.com/group/google-guice.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/166F8BEF-E599-4DE5-841A-6785A3BC4767%40gmx.ch.
For more options, visit https://groups.google.com/d/optout.
Loading...