【发布时间】:2013-12-22 17:01:48
【问题描述】:
我是第一次尝试使用 RequestFactory (RF),并且正在努力实现我的第一个 ServiceLocator。
来自RequestContext:
// Sign a user in or out of the app.
@ServiceName(
value="com.myapp.server.DefaultSignInOutService",
locator="com.myapp.server.DefaultSignInOutServiceLocator"
)
public interface SignInOutService extends RequestContext {
public Request<String> signIn(SignIn signIn);
public Request<Void> signOut(SignOut signOut);
}
然后是DefaultSignInOutServiceLocator:
public class DefaultSignInOutServiceLocator implements ServiceLocator {
// I am using Guice-3.0 for server-side DI, and ServiceLocatorModule is an AbstractModule.
ServiceLocatorModule serviceLocatorModule = new ServiceLocatorModule();
// Will be initialized by Guice.
private DefaultSignInOutService signInOutService;
public DefaultSignInOutServiceLocator() {
super();
// Bootstrap DI.
Injector injector = GWT.createInjector(serviceLocatorModule);
// injector.getInstance() returns a fully-configured/wired
// DefaultSignInOutService instance.
setSignInOutService(injector.getInstance(SignInOutService.class));
}
@Override
public Object getInstance(Class<?> clazz) {
// I'm trying to use proper DI best practices here, and avoid code like:
//
// return new DefaultSignInOutService(true, "Yes", 35);
//
// Rather, I'd like to be able to return an already pre-configured service impl:
return signInOutService;
}
// Getters/setters, etc.
}
我的理解是ServiceLocators 基本上是服务实现的工厂。如果这是真的,那么如果我将 Guice 用于服务器端 DI,我需要从定位器的构造函数中初始化我的 Guice 模块。但是,如果我需要自己编写任何代码(在应用程序的其他位置)创建DefaultSignInOutServiceLocator 的实例并显式调用其getInstance() 方法,那么我不需要将ServiceLocatorModule 放入@ 987654330@。在这种情况下,我可以有这样的代码:
public class DefaultSignInOutServiceLocator implements ServiceLocator {
@Injected
private DefaultSignInOutService signInOutService;
@Override
public Object getInstance(Class<?> clazz) {
return signInOutService;
}
// Getters/setters, etc.
}
所以这是我的问题:
-
ServiceLocator是否是放置 Guice 模块的合适位置(从而从其中引导 DI)?否则,如何使用正确连接/配置的服务实现注入定位器? - 或者,我只是不明白
ServiceLocator#getInstance()的目的吗? - 如果我在这里的方向是正确的,那么注入的
signInOutService应该是什么“范围”(Spring DI 术语)?它应该是单件还是多件/原型?我是否需要担心这里的线程安全(多个线程获得相同的signInOutService实例)?还是 GWT 以某种方式确保 RequestFactoryServlet 以线程安全的方式访问定位器?
【问题讨论】:
标签: java gwt guice requestfactory service-locator