Yes, though Injector::injectMembers will of course only work if you have a
handle on the Injector.
If you have an object whose creation is done completely outside of Guice,
then you have limited options on how to give that object a handle to the
injector.
And if you have the machinery to give that object an Injector, then you can
simply use that machinery to inject the members on it, as you point out.
The simplest solution for these cases, for better or worse, is often to
inject the Injector a static member of the class. Then, in the callback
method (which is 'bind()' in this case), you can use this handle to inject
your own member:
@Override public void bind() {
MyGuiScreenController.injector.injectMembers(this);
}
-Fred
On Wed, Nov 3, 2010 at 11:58 PM, Kartik Kumar <[email protected]>wrote:
> If you have a reference to already instantiated object, can't you use
> Injector#injectMembers(Object)
>
>
> On Wed, Nov 3, 2010 at 3:52 PM, David <[email protected]> wrote:
>
>> UPDATE:
>> In the few days of waiting for my post to pass moderation, the
>> developer of the 3rd party library contacted me. Turns out there IS
>> actually a way to pass an already instantiated object for the GUI to
>> use and call. He updated his wiki to document how. This basically
>> solves my problem though I am still curious about the question itself.
>>
>> Thanks,
>> ~David
>>
>>
>> On Oct 31, 7:33 pm, David <[email protected]> wrote:
>> > Alright I have read through the Guice user-guides and I am using Guice
>> > on several projects now. I am still pretty new to Guice though.
>> > Currently I have run into a bit of a snag and I wanted some advice on
>> > how to proceed. I don't know how to solve the following issue except
>> > by using static factories (which I know is frowned upon).
>> >
>> > Lets say I have a service, 'MyService', which is bound to
>> > 'MyServiceImpl', is injected into multiple places in my project, and
>> > is a Singleton (it has state that all parts of the program must be
>> > aware of). So far so good. This works as expected.
>> >
>> > Now one of the places I need access to this service is a class call
>> > 'MyGuiScreenController' which implements a third party interface,
>> > 'ScreenController'. Now here is where things get interesting. The
>> > rest of my code never touches MyGuiScreenController. It is
>> > instantiated (using a default, no argument constructor) by a third
>> > party (GUI) library via reflection. I have no control over this
>> > process. It has an inherited method 'bind' which is called when the
>> > GUI is setup, and then any number of methods I define based on GUI
>> > events.
>> >
>> > Now as I said, I could probably get away with a static factory and
>> > call said factory inside the bind method to get access to 'MyService',
>> > but is this the only way? It might get especially ugly as I will
>> > probably have many similar classes to 'MyGuiScreenController', all
>> > needing certain other service classes. Will I need to make a static
>> > factory for each one of these? That seems like a bad solution, but
>> > the only one I can figure out at the moment.
>> >
>> > Thanks for any help and please ask any questions if I did not explain
>> > myself fully.
>> >
>> > ~David
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "google-guice" group.
>> To post to this group, send email to [email protected].
>> To unsubscribe from this group, send email to
>> [email protected]<google-guice%[email protected]>
>> .
>> For more options, visit this group at
>> http://groups.google.com/group/google-guice?hl=en.
>>
>>
> --
> You received this message because you are subscribed to the Google Groups
> "google-guice" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<google-guice%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/google-guice?hl=en.
>
--
You received this message because you are subscribed to the Google Groups
"google-guice" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-guice?hl=en.