GIN and ClientBundle (Singleton or not-to-Singleton)?

172 views
Skip to first unread message

Daniel Guggi

unread,
Aug 17, 2011, 7:14:02 PM8/17/11
to google-we...@googlegroups.com
Hi,

I have some questions regarding ClientBundle and GIN.

Suppose the following blank bundle:

public interface MyClientBundle extends ClientBundle {
}

I want GIN to create the instance(es) of the MyClientBundle for me. The GIN-docs say (http://code.google.com/p/google-gin/wiki/GinTutorial#Deferred_Binding):

Note: Gin will not bind the instances created through GWT.create in singleton scope. That should not cause unnecessary overhead though, since deferred binding generators usually implement singleton patterns in their generated code.

Usage 1)
Don't specify MyClientBundle class in GinModule - this means that there is no bind(MyClientBundle.class) somewhere.
When injecting MyClientBundle into some beans, I see that there is always a new (not-singleton) instance of MyClientBundle -> as the gin-docs say

Usage 2)
Specify MyClientBundle class in GinModule as follows:

bind(MyClientBundle.class).in(Singleton.class);

When injecting MyClientBundle into some beans, I see that there is always the same (singleton) instance of MyClientBundle -> are the docs incorrect? or to i miss something here?

Usage 3)
Specifiy MyClientBundle class in a Gin Module as follows:

bind(MyClientBundle.class);

This leads to the following error:

com.google.inject.CreationException: Guice creation errors:
1) No implementation for gwtapp.client.navigation.MyClientBundle was bound.
  at gwtapp.client.navigation.NavigationModule.configure(NavigationModule.java:19)
1 error
    at com.google.inject.internal.Errors.throwCreationExceptionIfErrorsExist(Errors.java:416)



- imo the gin-docs are not correct here, because it is possible to create a ClientBundle with singleton-scope right? or do i miss something?
- what approach (usage1 vs usage2) would you recommend
- why does usage3 lead to an exception?

i use gin1.5

thank you,
daniel

Jens

unread,
Aug 18, 2011, 4:56:58 AM8/18/11
to google-we...@googlegroups.com
Some time ago I have used approach 2) and have bounded all ClientBundles as Singletons. But now I would go with 1) because when you look at the generated source code of a ClientBundle (use -gen <path> option) you will see that nearly everything in a ClientBundle implementation is static. So it doesn't really matter if its a Singleton or if you have several instances of it flying around.

As of your other questions: Its definitly possible to bind ClientBundles in a Singleton scope using .in(Singleton.class). As mentioned I have done this before. But I think you have missunderstood the documentation. I think it means: When you do not add any bind information for classes/interfaces that are used with deferred binding then Gin will just do a GWT.create() which obviously is not a singleton. But if you add bind information like bind(...).in(Singleton.class) then GIN will of course create Singletons because I think GIN will generate methods like:

YourClientBundle getCB() {
 if(cb == null) {
   cb = GWT.create(YourClientBundle.class);
 }
 return cb;
}

and uses this method to inject YourClientBundle into your classes.

-- J

Daniel Guggi

unread,
Aug 18, 2011, 5:47:01 AM8/18/11
to google-we...@googlegroups.com
thanks for clarification!

hf,
daniel


--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-web-toolkit/-/ZomN2fK33yIJ.
To post to this group, send email to google-we...@googlegroups.com.
To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

Reply all
Reply to author
Forward
0 new messages