RequestFactory don't play nice with guice?

265 views
Skip to first unread message

Simon Majou

unread,
Dec 4, 2010, 7:06:28 PM12/4/10
to Google Web Toolkit
When creating an object with RequestFactory the @inject fields are not
injected. I guess RequestFactory don't use guice to create the
objects. How can I delegate objects creation to Guice?

Thomas Broyer

unread,
Dec 5, 2010, 4:48:49 PM12/5/10
to Google Web Toolkit

Simon Majou

unread,
Dec 6, 2010, 8:56:07 AM12/6/10
to Google Web Toolkit
Thanks for the info.

On Dec 5, 10:48 pm, Thomas Broyer <t.bro...@gmail.com> wrote:
> On 5 déc, 01:06, Simon Majou <si...@majou.org> wrote:
>
> > When creating an object with RequestFactory the @inject fields are not
> > injected. I guess RequestFactory don't use guice to create the
> > objects. How can I delegate objects creation to Guice?
>
> "Just" wait for the 2.1.1 release and then implement a
> ServiceLayerDecorator.
> Seehttp://code.google.com/p/google-web-toolkit/wiki/RequestFactory_2_1_1
> andhttp://code.google.com/p/google-web-toolkit/source/browse/releases/2....
> Javadoc for each method can be find in the ServiceLayer class:http://code.google.com/p/google-web-toolkit/source/browse/releases/2....

Henrik Schmidt

unread,
Dec 26, 2010, 5:57:42 PM12/26/10
to Google Web Toolkit
Since GWT 2.1.1 is here:

Another way of doing it would be to just use a normal ServletModule as
per the Guice user guide: http://code.google.com/docreader/#p=google-guice&s=google-guice&t=ServletModule

Then bind the RequestFactoryServlet;

bind(RequestFactoryServlet.class).in(Singleton.class);
serve("/gwtRequest").with(RequestFactoryServlet.class);

Otherwise following the RequestFactory documentation you can use:

requestStaticInjection(MyDomainClass.class)

in your module to inject the PersistenceManager (or EntityManager)
statically.

Thomas Broyer

unread,
Dec 27, 2010, 1:35:45 PM12/27/10
to google-we...@googlegroups.com
For information:
  • I use Guice's ServletModule to bind my RequestFactory servlet
  • to benefit from Guice in Service-s and Locator-s, I use a very simple ServiceLayerDecorator, that I inject into a RequestFactoryServlet subclass (so I can pass it to the super() constructor). The ServiceLayerDecorator is being injected the Injector, and overrides createServiceInstance and createLocator
  • createLocator is implemented by directly calling the Injector's getInstance
  • because of the way it's done, createServiceInstance first calls getTop().resolveServiceLocator. If it's not null, then it get's an instance from the Injector and calls getInstance on it (with the domainMethod.getDeclaringClass() as argument, to mimic the default behavior). That way, the ServiceLocator is dependency-injected.
  • Because I want my services to be dependency-injected (and not necessarily my ServiceLocator-s), I created an InjectingServiceLocator that get's injected the Injector and then uses injector.getInstance to implement the ServiceLocator#getInstance. You the just have to use that ServiceLocator for your service for it to be dependency-injected by Guice.
It all fits in approx. 20 LoC.

Daniel Dietrich

unread,
Aug 31, 2011, 8:37:46 PM8/31/11
to Google-We...@googlegroups.com
Wow, that works fine for me. Thanks a lot!


Reply all
Reply to author
Forward
0 new messages