我将GWT 2.2与RequestFactory一起使用。该应用程序具有一个现有的服务层(服务器端),因此我正在使用ServiceLocator提供这些实现。我的Proxy和RequestContexts指定要使用的正确服务和定位符(如here所示)。我可以提出基本的数据请求,但是当我尝试保存时,出现以下异常:
com.google.gwt.requestfactory.server.UnexpectedException: Could not instantiate Locator com.schedgy.core.service.OrganizationService. Is it default-instantiable?
at com.google.gwt.requestfactory.server.ServiceLayerDecorator.die(ServiceLayerDecorator.java:185)
at com.google.gwt.requestfactory.server.LocatorServiceLayer.newInstance(LocatorServiceLayer.java:222)
at com.google.gwt.requestfactory.server.LocatorServiceLayer.createLocator(LocatorServiceLayer.java:47)
at com.google.gwt.requestfactory.server.ServiceLayerDecorator.createLocator(ServiceLayerDecorator.java:54)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
OrganizationService的定义如下:
// Parent class provides the generic Locator<Organization, String> methods
public class OrganizationService extends CompanyEntityService<Organization> {
protected OrganizationDao organizationDao;
protected UserDao userDao;
protected RoleDao roleDao;
@Inject
public OrganizationService(
OrganizationDao organizationDao,
UserDao userDao,
RoleDao roleDao) {
super(organizationDao, Organization.class);
this.organizationDao = organizationDao;
this.userDao = userDao;
this.roleDao = roleDao;
}
... additional methods
}
我的定位器类如下所示:
public class CompanyServiceLocator implements ServiceLocator {
protected Injector injector;
public CompanyServiceLocator() {
injector = GuiceFactory.getInjector();
}
@Override
public Object getInstance(Class<?> clazz) {
return injector.getInstance(clazz);
}
}
OrganizationProxy看起来像:
@ProxyFor(value=Organization.class, locator=OrganizationService.class)
public interface OrganizationProxy extends CompanyEntityProxy {
... setters/getters defined here
}
OrganizationRequest看起来像:
@Service(value=OrganizationService.class, locator=CompanyServiceLocator.class)
public interface OrganizationRequest extends RequestContext {
...
}
客户端代码如下所示:
OrganizationRequest req = organizationRequestFactory.request();
req.paginate(0, 10).fire(paginationReceiver); // Works!
req = organizationRequestFactory.request();
OrganizationProxy org = req.create(OrganizationProxy.class);
org.setName("test");
req.save(org).fire(receiver); // This causes the server side exception
对我来说很明显,ServiceLayerDecorator无法实例化OrganizationService,因为它没有默认的构造函数,但这就是为什么我使用Guice并重写了ServiceLocator以使用Guice创建服务实例的原因。但是,为什么第一个调用正确使用了我的ServiceLocator,而第二个却没有呢?
最佳答案
定位符必须是默认实例化的。
@ProxyFor(value=Organization.class, locator=OrganizationService.class)
这是事情脱离轨道的地方。如果
OrganizationService
出售Organization
的实例以实现Locator
接口,则需要使其默认不可实例化或注入实现ServiceLayerDecorator
的createLocator()
。第一个代码示例而不是第二个代码示例起作用的原因是第二个代码示例正在基于来自客户端的命令创建和变异
Organization
。在这种情况下,Locator.create()
必须由RequestFactory服务器代码调用。在不知道paginate()
返回给客户端的情况下,我怀疑没有返回任何Organization
实例,因为有必要调用Locator.getId()
和Locator.getVersion()
方法。