guice and gwt (server side rpc)

531 views
Skip to first unread message

ich du

unread,
Jul 1, 2011, 7:53:47 AM7/1/11
to google...@googlegroups.com
i have one short question: is this the current (gwt 2.3, guice 3.0) way to integrate guice and gwt on server side? (don't know why but i have bad feeling about overriding processCall)?!
If not is there a current tutorial how to integrate both?

thx in advance

jMotta

unread,
Jul 1, 2011, 8:13:44 AM7/1/11
to google...@googlegroups.com
Ich Du,

It can work, but I would advice you to try something less intrusive and full of tweaks as it is. I use a ServletModule of Guice to serve my servlets and filters.

The GWT servlets responsible for proccessing the requests incoming from the RPC framework are just servlets. You map them in the servlet module, using the same context name as the module created in GWT (e.g.: if your module call myModule your context will also be myModule) following this pattern:

The mapping in the servlet module will be: /<module name>/<url pattern>
The servlet that you bind to the above url pattern must extends RemoteServiceServlet and implements the interface below
The service interface that you create altogether with the async one must have the annotation @RemoteServiceRelativePath("<url pattern>")

Once everything is "wired" you must add the servlet module to your injector and that's it, you'll be able to call this service from you GWT application and also use @Inject as any other class mapped to use DI. :)

Here is an exemple of it:

// This is the service interface that is created with the async version. This is a requirement of the RPC framework.
@RemoteServiceRelativePath("prototype")
public interface PrototypeService extends RemoteService {
....
}

// This is the GWT servlet that will proccess my RPC calls!
@Singleton
public class PrototypeServiceImpl extends RemoteServiceServlet implements PrototypeService {

private Objectify objectify;

@Inject
public PrototypeServiceImpl(Objectify objectify) {
this.objectify = objectify;
}

...
}

// This is the servlet module that will map my url patterns to my servlets. Note that the web.xml will not be used to map
// it anymore. If you want your servlets and filters to be DI-aware then you must map them here.
public class ServletModule extends com.google.inject.servlet.ServletModule {
@Override
protected void configureServlets() {
serve("/bus/prototype").with(PrototypeServiceImpl.class);
}
}

I hope it helps you. :)

Jayr Motta
Software Developer




On Fri, Jul 1, 2011 at 8:53 AM, ich du <tant...@hotmail.com> wrote:
i have one short question: is thi s the current (gwt 2.3, guice 3.0) way to integrate guice and gwt on server side? (don't know why but i have bad feeling about overriding processCall)?!

If not is there a current tutorial how to integrate both?

thx in advance

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

ich du

unread,
Jul 1, 2011, 8:28:25 AM7/1/11
to google...@googlegroups.com
thx for clarification,

your example feels much better :-)

but why the article mentioned above goes this way?:

 the "GuiceRemoteServiceServlet" is used to bundle all rpc requests in one servlet. so the "extends RemoteServiceServlet"  isn't needed for all other service implementations. The servlet module looks than like this
...
serve("servletPath").with(GuiceRemoteServiceServlet.class)
...

jMotta

unread,
Jul 1, 2011, 8:36:41 AM7/1/11
to google...@googlegroups.com
Because he choose to decide where to go when the request arrives to the server inside that servlet mapped. It will surely have pros and cons over the alternative that I gave you.

I prefer the one that I sent because it's cleaner and with less workarounds. I'll think and study it trying to find the pros and cons, but both will work, it's just a matter of taste.

Jayr Motta
Software Developer




--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-guice/-/SysVOuPg8JYJ.

ich du

unread,
Jul 1, 2011, 9:08:33 AM7/1/11
to google...@googlegroups.com
thx again,

i just tried to wire up my server side with guice - the rpc part is done so far (thx to you). but on server side i also have some pojo interfaces/implementation that i want to inject via guice. but how to bootstrap this? at the moment i just added the module to my GuiceServletContextListener:

public class AppGuiceContextListener extends GuiceServletContextListener {

    @Override
    protected Injector getInjector() {
        return Guice.createInjector(new ServerModule(), new ServletModule());
    }

}

(not later than now i have to admit i am a total guice noob)
but imho somehow i need an injector instance on server side to get instances of classes that use injection?! something like that:
Injector i =
Guice.createInjector(new ServerModule(), new ServletModule());
ClassThatHasSom@InjectInside instance = i.getInstance(
ClassTh...@InjectInside.class);

So on Server i need a reference to the injector instance?! is it a good idea to store the injector in a (static?) field in AppGuiceContextListener or should i just call getInjector() whenever i need it?

jMotta

unread,
Jul 1, 2011, 9:27:40 AM7/1/11
to google...@googlegroups.com
Ich Du,

No, what you need is to create a module to "normal" injections, the one that I've told you is a special one that deals with the special characteristics of a servlet.

The classes that will be inject must be also be mapped by Guice, you will need something like this:

public class MainModule extends AbstractModule {
@Override
protected void configure() {
bind(Objectify.class).toProvider(ObjectifyTransacionalProvider.class);
bind(Objectify.class).annotatedWith(Names.named("Non-Transactional")).toProvider(ObjectifyProvider.class);
bind(ObjectifyFactory.class).toInstance(ObjectifyService.factory());
...
}
}

And then install this module with the servlet module that you've installed before. I recommend you to read the guides in the Guice page, they're very clear. :)

Jayr Motta
Software Developer




--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-guice/-/orv5N_ikTo4J.

ich du

unread,
Jul 1, 2011, 10:26:23 AM7/1/11
to google...@googlegroups.com
don't know if this is a double post (often i got error 316 on submitting a post or it is submitted but it is not displayed/posted properly?!)
anyway i try again:

thx jayr but my problem is not on how to create MainModule and some classes that use injection. my problem is how to get an reference to injector. all examples are showing some main-method with
Injector i = Guice.createInjector(new MainModule());
MyClass mc = i.getInstance(MyClass.class);

but in case of guice -ing gwt rpc the incetor is instantiated in GuiceServletContextListener. My Question is: Should i create an separate injector for MainModule (i guess not)? or should i save the injector created in GuiceServletContextListener in a field (static?!) accessible via a second public/static method?

ich du

unread,
Jul 1, 2011, 9:51:09 AM7/1/11
to google-guice
thx but as i mentioned in previous post, i already got a "MainModule"
called ServerModule. My Problem is how to use it on Serverside. All
Examples doing the bootstrapping in main - cretaeInjector,
getInstance. But in the guice/gwt/rpc case the injector instance is
created in GuiceServletContextListener. With this i can't get an
injector where i want it. in my special case my servlets need such an
injected class:

public class MyServiceImpl extends RemoteServiceServlet implements
MyService {

@Override
public ArrayList<SomeClass> getList() {
return injector.getInstance(Dataprovider.class).getList();
}
}

But how to get an Injector instance at this place? Should i create an
injector wherever i need one? or save the injector created inside my
GuiceServletContextListener and make it available via a getI()-Method?

On Jul 1, 3:27 pm, jMotta <jayrmo...@gmail.com> wrote:
> Ich Du,
>
> No, what you need is to create a module to "normal" injections, the one that
> I've told you is a special one that deals with the special characteristics
> of a servlet.
>
> The classes that will be inject must be also be mapped by Guice, you will
> need something like this:
>
> public class MainModule extends AbstractModule {
> @Override
>  protected void configure() {
> bind(Objectify.class).toProvider(ObjectifyTransacionalProvider.class);
>
> bind(Objectify.class).annotatedWith(Names.named("Non-Transactional")).toProvider(ObjectifyProvider.class);
> bind(ObjectifyFactory.class).toInstance(ObjectifyService.factory());
> ...
>
> }
> }
>
> And then install this module with the servlet module that you've installed
> before. I recommend you to read the guides in the Guice page, they're very
> clear. :)
>
> *Jayr Motta*
> Software Developer
> *
> *
> I'm  on BlackBeltFactory.com<http://www.blackbeltfactory.com/ui#!User/jmotta/ref=jmotta>
> !
>
>
>
>
>
>
>
> On Fri, Jul 1, 2011 at 10:08 AM, ich du <tantea...@hotmail.com> wrote:
> > thx again,
>
> > i just tried to wire up my server side with guice - the rpc part is done so
> > far (thx to you). but on server side i also have some pojo
> > interfaces/implementation that i want to inject via guice. but how to
> > bootstrap this? at the moment i just added the module to my
> > GuiceServletContextListener:
>
> > public class AppGuiceContextListener extends GuiceServletContextListener {
>
> >     @Override
> >     protected Injector getInjector() {
> >         return Guice.createInjector(new ServerModule(), new
> > ServletModule());
> >     }
>
> > }
>
> > (not later than now i have to admit i am a total guice noob)
> > but imho somehow i need an injector instance on server side to get
> > instances of classes that use injection?! something like that:
> > Injector i = Guice.createInjector(new ServerModule(), new
> > ServletModule());
> > ClassThatHasSom@InjectInside instance = i.getInstance(
> > ClassThatHas...@InjectInside.class);

jMotta

unread,
Jul 1, 2011, 10:00:21 AM7/1/11
to google...@googlegroups.com
Ich Du,

You do not need an instance of the injector, that's the magic on DI. Let's say you map a constructor annotated with @Inject and some of the objects that you inject also have inside them constructors annotated with @Injected and so on.

When your servlet is created the injector will take care to inject every mapped object in the annotated constructors / fields / setters recursively until the bottom of your object graph. Got it?

Otherwise the whole loosed coupling would have gone once you would need to create a dependency of your injector and also from the class injected even not creating them using the new operator. This is similar to the factory pattern.


Jayr Motta
Software Developer




jhulford

unread,
Jul 2, 2011, 2:47:11 PM7/2/11
to google-guice
If you don't agree w/ jMotta and feel you really need the injector in
your sevice implementation, you can just inject it into your service.
Guice creates a default binding for the injector, so you don't even
have to set one up in your module.
> > bind(Objectify.class).annotatedWith(Names.named("Non-Transactional")).toPro­vider(ObjectifyProvider.class);
> > >http://groups.google.com/group/google-guice?hl=en.- Hide quoted text -
>
> - Show quoted text -

ich du

unread,
Jul 4, 2011, 2:21:48 AM7/4/11
to google...@googlegroups.com
i don't know if i nee the injecotr but i need an instance of a class (to call a method on it). this class has an inject on constructor. all examples i saw uses injector to get an instance of the class. some example talk about "bootstrapping" - in main method they create an injector (). in servlet case the injector is created in "GuiceServletContextListener" - and i guess without this injector instance nothing will work. so with this in place on first rpc call a injector is created and injection happens.

My problem is how to call a method on an instance if i don't have the instance? as i mentioned before i have 2 modules one for servlets and one for all other stuff. i need an instance of other stuff. ok here is an example:

my RemoteServiceServlet:

...
 public ArrayList<> getSome(){
return (some class with inject on constructor).getSome();
}

at the moment it looks like:

return ServerInjector.GET.instance().getInstance(GetSomeClass.class).getSome();

At the moment i have a singleton injector on server side and the method above and the Listener use this injector. so how could this work without injector - (i thought to inject a dependency via guice i need at least a place for injection - an construcot or method with instance as parameter?!)

jhulford - please tell me what you mean with "just inject it into your service" - i am a total DI noob

William Temperley

unread,
Jul 4, 2011, 5:39:31 AM7/4/11
to google...@googlegroups.com
Ich,

Instead of using the injector, you can use a provider. Guice will
inject a provider of the class you need, if you ask it to. For
instance in the following code if your servlet is bound with Guice, a
provider of your GetSomeClass will be injected for you (and you won't
need to write the provider either).


@Singleton
public class SomeServiceImpl extends RemoteServiceServlet implements
SomeService {

private Provider<GetSomeClass> getSomeClassProvider;

@Inject
public SomeServiceImpl(Provider<GetSomeClass> getSomeClassProvider) {
this.getSomeClassProvider= getSomeClassProvider;
}

public ArrayList<> getSome(){
return getSomeClassProvider.get().getSome();
}

> --
> You received this message because you are subscribed to the Google Groups
> "google-guice" group.
> To view this discussion on the web visit

> https://groups.google.com/d/msg/google-guice/-/vXyhIZZ5OWMJ.

ich du

unread,
Jul 4, 2011, 10:06:43 AM7/4/11
to google...@googlegroups.com
thx!
thats simply great - my construction didn't felt proper. is there a guide/example using "provider" - as i mentioned before all examples i saw so far created an injector in main method and use this instance. do i still need the injector (in such cases with main method)?

William Temperley

unread,
Jul 4, 2011, 10:31:41 AM7/4/11
to google...@googlegroups.com
In my fairly limited experience with Guice, in general you'll only
need to talk to the injector once per application to obtain the
class(es) that have overall control of the application.
These classes are therefore Guice managed and the rest of the classes
are injected or provided by Guice. So in a main method you might do
injector.getAppController() or something. In Guice-servlet, the only
time you really need the injector is in the GuiceContextListener.

> --
> You received this message because you are subscribed to the Google Groups
> "google-guice" group.
> To view this discussion on the web visit

> https://groups.google.com/d/msg/google-guice/-/C0tASrhbBtEJ.

alex opn

unread,
Jul 5, 2011, 10:47:12 PM7/5/11
to google...@googlegroups.com
http://www.manning.com/prasanna/
Dependency Injection is a great book about DI, although it's not
dealing with Guice 3.0 the main concepts and a lot of deeper stuff are
still covered very good and have not changed anyways.
Reply all
Reply to author
Forward
0 new messages