RequestFactory Injection in the Locator

67 views
Skip to first unread message

Sydney

unread,
Jun 19, 2011, 12:04:02 PM6/19/11
to google-we...@googlegroups.com
I need to inject a DatastoreObject (Twig) into a Locator. The problem is that the Locator is created by a clazz.newInstance() method so the default constructor is used.

public class TwigLocator extends Locator<DatastoreObject, Long> {

   private ObjectDatastore datastore;

   public TwigLocator() {
       System.out.println(datastore);
   }

   @Inject
   public TwigLocator(final ObjectDatastore datastore) {
       this.datastore = datastore;
   }

}

From LocatorServiceLayer

 private <T> T newInstance(Class<T> clazz, Class<? super T> base) {
   Throwable ex;
   try {
     return clazz.newInstance();
   } catch (InstantiationException e) {
     ex = e;
   } catch (IllegalAccessException e) {
     ex = e;
   }
   return this.<T> die(ex,
       "Could not instantiate %s %s. Is it default-instantiable?",
       base.getSimpleName(), clazz.getCanonicalName());
 }

Do you know how I can modify the way the Locator is created? I guess creating the Locator this way would call the constructor with injection public TwigLocator(final ObjectDatastore datastore)

private <T> T newInstance(Class<T> clazz, Class<? super T> base) {

return GuiceConfig.getInjectorReference().getInstance(clazz);

}

Thanks

Thomas Broyer

unread,
Jun 19, 2011, 1:20:39 PM6/19/11
to google-we...@googlegroups.com
The "newInstance" is the default behavior, which you can override using your own ServiceLayerDecorator. Have a look at https://github.com/etiennep/injected-requestfactory for an example using Guice.
Keep in mind though that everything created by RequestFactory's server-side code (except domain objects of course; i.e. locators, service locators and services) are kept in a static map so they technically are singletons (almost: they're referenced using SoftReferences, so they can be reclaimed if the server needs to free memory, and then a new instance will be created later on, when needed: this is the ServiceLayerCache)
Reply all
Reply to author
Forward
0 new messages