Sharing an instance between multiple Ginjectors

293 views
Skip to first unread message

Raphaël POCHET

unread,
Sep 29, 2010, 2:06:51 PM9/29/10
to google-gin
Hi everyone

I have a problem here, i got 2 ginjectors, each one declaring MVP
presenters relying on the same EventBus instance.

One is low-level (bootstrap, system, eventBus...) and one is top-level
(presenters, features).

So in the LOW-level GinModule i tell Gin how to bind the event bus :
bind(EventBus.class).to(DefaultEventBus.class).in(Singleton.class);

But in the TOP-level GinModule i am declaring my presenters who do
need the EventBus.

So i got an error saying there is no method getEventBus in the TOP-
level ginjector. If i declare one and use the same binding as in the
LOW-level ginjector i got 2 different instances of the EventBus which
is not what i want.

Question 1 : is there a way in a module to tell GIN to use the
instance made by an other Ginjector ?
Question 2 : if answer is no, and assuming the TOP ginjector will
extends the LOW ginjector (to share same instance), is there a way to
GWT.create both ? Because the TOP ginjector is not available in the
classpath of the project containing the LOW-level ginjector. Thus, i
need to get my low-level elements in the bottom project, and i need my
eventbus in the top-level project...

Actually i am avoiding to use directly the ginjector.getStuff()
methods and i am injecting averything i need in the constructors, but
i can't do that for everything...

I hope you understood me ^^

Thanks for reading and for whatever insight you can provide !

Jeff Larsen

unread,
Sep 29, 2010, 2:47:04 PM9/29/10
to google-gin
Why do you need them both seperated? You can add multiple modules to a
ginjector, so what about just adding the feature module to the
ginjector.

Also the error your getting makes me suspicious that you're using
gwtp, which requires several methods in the ginjector (look at the
sample applications to know which ones).

Thomas Broyer

unread,
Sep 29, 2010, 3:57:29 PM9/29/10
to google-gin


On 29 sep, 20:47, Jeff Larsen <larse...@gmail.com> wrote:
> Why do you need them both seperated? You can add multiple modules to a
> ginjector, so what about just adding the feature module to the
> ginjector.

You can also have TopLevelInjector extends LowLevelInjector. Or you
can install() the LowLevelModule from within the TopLevelModule.
In any case, you'd only GWT.create the TopLevelInjector, never
directly the LowLevelInjector.

Raphaël POCHET

unread,
Sep 30, 2010, 4:44:11 AM9/30/10
to google-gin
Thanks for your answers !

Indeed, in any case i am forced to GWT.create only the top level
Ginjector, preventing the low-level project to have a ginjector
accessible in his classpath.

I need them both separated for multiple reasons. Essentially, there is
a framework project which will be used by different client projects. I
want in my framework project to be able to do :
Fmk.ginjector.getEventBus() (or something else). But i can't do that
without GWT.creating the ginjector in my framework project
explicitely. That's why i neither want my top level ginjector to
extends my framework ginjector nor my top level ginjector to reference
the framework module.

If i install() the framework module in the top level module the result
is the same.

I am indeed using gwtp. I know the framework and how it works, my
projects are running fine since a long time, but as the framework
grows in complexity, i need more access to my low level ginjector.

As i said i can use the @Inject annotation in my framework objects to
retrieve what i need, but i'll prefer to have an hand on my ginjector
at this point.

Is there in Gin no method to retrieve a ginjector without being in Gin
scope ?
Something like : Gin.retrieveGinjector("ginjectorName") > which i
could then cast in FmkGinjector for example.

Thanks for reading !

Raphaël POCHET

unread,
Sep 30, 2010, 7:59:04 AM9/30/10
to google-gin
I just found than the following code is working :

public class FmkGinjectorHolder {

@Inject
public FmkGinjectorHolder(FmkGinjector ginjectorInstance) {
// It works !
ginjectorInstance.getFrameworkServiceOrSomethingElse();
}

}

That's what i need, since it allows me to have my top-level project
ginjector to simply extends my framework ginjector.

Thomas Broyer

unread,
Sep 30, 2010, 9:07:33 AM9/30/10
to google-gin


On Sep 30, 1:59 pm, Raphaël POCHET <pochyp...@gmail.com> wrote:
> I just found than the following code is working :
>
> public class FmkGinjectorHolder {
>
>      @Inject
>      public FmkGinjectorHolder(FmkGinjector ginjectorInstance) {
>                // It works !
>                ginjectorInstance.getFrameworkServiceOrSomethingElse();
>      }
>
> }
>
> That's what i need, since it allows me to have my top-level project
> ginjector to simply extends my framework ginjector.

That's what I was about to propose: inject your Ginjector (and pray
for the AppGinjector to be passed when a FmkGinjector is needed, and
not injecting a GWT.create()d FmkGinjector).
Glad to see it work!
Did you need to add something to your AppGinjector or is it all
"magical"?

Peter Schmitt

unread,
Sep 30, 2010, 11:09:51 AM9/30/10
to googl...@googlegroups.com
It's "magical" - a ginjector will indeed inject itself if so requested. :)


--
You received this message because you are subscribed to the Google Groups "google-gin" group.
To post to this group, send email to googl...@googlegroups.com.
To unsubscribe from this group, send email to google-gin+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-gin?hl=en.


István Szoboszlai

unread,
Sep 30, 2010, 9:21:44 AM9/30/10
to googl...@googlegroups.com
Hello,

Have you tested it compiled? To me happend with gin, that it worked in hosted mode, but not after gwt compile.


Üdvözlettel / Best Regards
--
István Szoboszlai | istvan.s...@inepex.com
 http://inepex.com | IT Development and Location Based Services


Raphaël POCHET

unread,
Sep 30, 2010, 12:13:24 PM9/30/10
to google-gin
Hello

I didn't tested it beyond hosted mode since i've finally changed my
mind : the top level project will have to call a method in his
onModuleLoad() : Framework.startup(FmkGinjector) which will allow me
to store and use the ginjector in my framework project at the cost of
a line of boilerplate code in the top level project. It's a fair
compromise.

Thanks to everyone for the help and the support provided :)

On 30 sep, 15:21, István Szoboszlai <ist...@szoboszlai.eu> wrote:
> Hello,
>
> Have you tested it compiled? To me happend with gin, that it worked in
> hosted mode, but not after gwt compile.
>
> Üdvözlettel / Best Regards
> --
> István Szoboszlai | istvan.szobosz...@inepex.com
>  http://inepex.com| IT Development and Location Based Services
>
>  <http://inepex.com/>
>
>
>
> On Thu, Sep 30, 2010 at 3:07 PM, Thomas Broyer <t.bro...@gmail.com> wrote:
>
> > On Sep 30, 1:59 pm, Raphaël POCHET <pochyp...@gmail.com> wrote:
> > > I just found than the following code is working :
>
> > > public class FmkGinjectorHolder {
>
> > >      @Inject
> > >      public FmkGinjectorHolder(FmkGinjector ginjectorInstance) {
> > >                // It works !
> > >                ginjectorInstance.getFrameworkServiceOrSomethingElse();
> > >      }
>
> > > }
>
> > > That's what i need, since it allows me to have my top-level project
> > > ginjector to simply extends my framework ginjector.
>
> > That's what I was about to propose: inject your Ginjector (and pray
> > for the AppGinjector to be passed when a FmkGinjector is needed, and
> > not injecting a GWT.create()d FmkGinjector).
> > Glad to see it work!
> > Did you need to add something to your AppGinjector or is it all
> > "magical"?
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "google-gin" group.
> > To post to this group, send email to googl...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > google-gin+...@googlegroups.com<google-gin%2Bunsubscribe@googlegrou ps.com>
> > .

javadude79

unread,
Jul 13, 2012, 5:01:30 PM7/13/12
to googl...@googlegroups.com
Hi Raphael:

I have a similar issue in my gwt 2.4 project with two modules. Module A inherits Module B. Each module has its own gin module and ginjector and I'd like to share an EventBus and MVP classes between the two modules. Since B is independent of A it would make sense to pass the common classes from Module A. 

I'm still unsure about your solution. How would a framework level injector be created from the parent injector? If you have a code sample to share, I'd appreciaite.

I posted a similar question on stackoverflow for your reference.

Thanks.
Reply all
Reply to author
Forward
0 new messages