|
GinFaq
GIN frequently asked questions
Featured GIN frequently asked questionsHow does GIN work?GIN uses Guice at compile-time via a GWT Generator. The generator creates an implementation of your Ginjector interfaces. Does GIN have runtime overhead?No, it shouldn't -- it essentially generates the exact code you'd use if you did the dependency injection and object construction by hand. You can look for yourself by compiling your GWT application with PRETTY mode and looking at the JavaScript. What bindings don't work?Because your Module class is actually executed at compile time, you have to make sure its configure(...) method does not execute any GWT client-side code directly. And thus, you also can't carry along any object instances from the Module in to the eventual client code. So, these don't work:
While this does: class MyModule extends AbstractGinModule {
@Override
protected void configure() {
bind(Something.class).toProvider(SomethingProvider.class);
}
static class SomethingProvider implements Provider<Something> {
public Something get() {
return new Something(getDataFromJavaScript());
}
// Using JSNI in a provider
private native String getDataFromJavaScript() /*-{
...
}-*/;
}
}Note that you will not be able to reuse your GinModule on the server side (using GinModuleAdapter) if your bindings directly or indirectly make use of GWT specific infrastructure like GWT.create(...), widgets or JSNI. See the GuiceCompatibility page for a feature comparison with Guice. How do I use GIN with GWT RPC?There are two options:
The automatic binding works by replacing an injection of MyRemoteServiceAsync with GWT.create(MyRemoteService.class). When using this mechanism, to set the URI used for the remote service, put @RemoteServiceRelativePath on interface MyRemoteService. Why do I get a [ERROR] Deferred binding result type 'A' should not be abstract error during compile?This can happen when Gin tries to instantiate an interface by calling GWT.create on it - a method Gin uses to allow easy injection of interfaces configured with GWT's deferred binding mechanism. Unfortunately, if no deferred binding or replacement is registered for the interface, you will get the above error. To fix the problem, bind the offending interface to an injectable implementation in your GinModule. Note: This can also happen when using @Optional injection - if the injected parameter is of an interface type and the interface is not bound, Gin will not use optional injection but call GWT.create on the interface, potentially causing this error. Note: Please don't ask questions in the comments below but join and ask the GIN discussion group instead. We won't answer questions in the comments! |
How about adding Where do I get gin.jar from and what is aopalliance.jar
You have to build gin.jar for the moment. Im in the middle of that troublesome procedure right now.
If I'm not running a GWT-RPC server, will I still be able to use Gin? It seems as though it's essentially compiled in and only has the Java dependency at compile-time, but I would just like to make sure.
You don't need to run a GWT/Java/Guice server, all GWT client code will work with Gin, regardless of its server.
Building gin.jar works fine. I having trouble locating documentation on how to use it on in the client under Eclipse. My code compiles and tests just fine, however, when I try to run the Eclipse Web Application with Ginjector code in the client it breaks:
ERROR? No source code is available for type com.google.gwt.inject.client.Ginjector; did you forget to inherit a required module?
@jSlim180,
It looks like you're forgetting to inherit the GIN GWT module. You need to add the following to your GWT module file:
<inherits name="com.google.gwt.inject.Inject" />
@jSlim180, Have had the same error. I guess it should be added to this FAQ because it is not clear. In order to add GIN to your project you should add 3 files to build path: 1) gin.jar 2) guice.jar (from guice distro) 3) aopalliance.jar (from guice distro)
I can't build the GIN jar, when will you propose it ? Can someone propose it somewhere please ?
Someone's providing nightly builds: http://code.google.com/p/google-gin/issues/detail?id=45#c3
It is possible to use GIN and GUICE with an application with GWT and JSON ? thanks for your answer.
Currently, in my project, I'm using replace-with to handle some specific implementations for gecko1_8. But with gin, I haven't found any way to do it.
The scenario is like, I have RegisterPanel?, and its subclasses RegisterPanelStandard? for other browsers and RegisterPanelGecko? for firefox.
I have Module.java
public class Module extends AbstractGinModule { @Override protected void configure() { bind(RegisterPanel.class).to(RegisterPanelStandard .class); } }and ModuleGecko?.java
public class ModuleGecko extends Module { @Override protected void configure() { super.configure(); bind(RegisterPanel.class).to(RegisterPanelGecko .class); } }and Injector.java
@GinModules(Module.class) public interface Injector extends Ginjector { RegisterPanel getRegisterPanel(); }and in myproject.gwt.xml, I added the following,
<replace-with class="com.topcmm.client.ModuleGecko"> <when-type-is class="com.topcmm.client.Module"></when-type-is> <when-property-is name="user.agent" value="gecko1_8"></when-property-is> </replace-with>But I found it doesn't work for me. Anyone can explain it or how can I make it work?
Appreciate for any comments!
Hey everyone,
We are trying to split out our codebase into multiple GWT modules with GIN for dependency injections. A need arose for different Ginjectors to inherit from other Ginjectors.
We found that this works:
@GinModules(ParentModule.class) public interface ParentInjector extends ChildInjector, Ginjector{ public SomeClass getSomeClass(); }and then the ChildInjector? is like this:
@GinModules(ChildModule.class) public interface ChildInjector extends Ginjector{ public SomeOtherClass getSomeOtherClass(); }Now, we can get the SomeOtherClass? instance by instantiating the parent Ginjector:
This works fine, but I'm wondering whether we're overlooking any major issues this could cause?
Any comments are appreciated!
@vkubushyn i'm having the exact same problem.
How does lazy instantiation works with jin?
Can I inject a parametrized type to a class using jin? I tried several approaches but I get error
Sorry for answering a question in the comments but....
@arashbi Lazy instantiation works fine. You can see this by adding some sort of message, such as Window.alert("XXX"), to the constructor of a class created by gin. You will see that the message only appears when the injector.xxx() method is called. By printing out hashCodes you can also check that Singleton etc. works as you expect.
For parameterized types use the TypeLiteral? class, take a look at the Guice documentation for this.
While working on a gin module in development mode, I often get the "Deferred binding result type 'A' should not be abstract" error. After restarting the development console, it's gone. Just a hint in case you run into this one as well.
Debian, 2.6.26-2 Firefox 3.6.13 GWT 2.1.1 GIN 1.0
I have a question to Gin team:
In Guice, I specify the module when I am creating an injector. This is fine because I can create two injectors with different modules, for testing purposes.
In Gin, I must specify the module to use in the Injector interface via @GinModules?. So I cannot create two instances of the injector with different modules. This creates a dependency from an interface to the implementation and defeats the whole purpose of dependency injection.
Is there a way around it?
There are two ways to reuse Ginjectors. The classic way is to subclass them, i.e. if the ginjector with all your methods is A, then have 2 (n) ginjectors B and C, each of which extend A but which define different gin modules.
Another way to easily swap the gin module names is to use GWT properties, which was recently implemented. Take a look at http://code.google.com/p/google-gin/source/browse/trunk/src/com/google/gwt/inject/client/GinModules.java for some more documentation.