在google Guice中,我可以使用该功能创建基于多个模块的注入器createInjector.
因为我用来GWT.create在GoogleGin中实现注入器,是否可以基于多个创建一个Ginjector AbstractGinModule.
如果我们不能,您如何组织代码以避免在同一个模块中拥有所有绑定?
我有什么理由不能在GWT应用程序中使用Guice for DI吗?如果是这样,为什么?如果没有,那么GWT-GIN 真正提供的是POG(普通的'Guice)没有?
我不确定我是否完全理解依赖注入背后的想法,特别是使用Guice.
我有很大的摇摆应用程序,我想引入guice,来解耦这个应用程序.假设我在主要班级有注射器
Guice.createInjector(new BindingModule());
Application app = injector.getInstance(Application.class);
app.run();
Run Code Online (Sandbox Code Playgroud)
它有效.如果我有一些字段,让我们说在应用程序类中的JPanel,用@Inject注释然后注入它.但是如果我在Application构造函数中手动创建一些东西,那么将不会注入示例中的JTree(假设一切都配置正确).
class Application {
@Inject JPanel mainPanel //this will be injected
JPanel otherPanel;
public Application() {
otherPanel = new MyNewPanel();
mainPanel.add(otherPanel);
}
}
class MyNewPanel extends JPanel {
@Inject JTree tree; //this will not be injected
public MyNewPanel() {
add(tree);
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,我是否需要控制注射的所有注射物体.我不能像我一样打破控制otherPanel.
使用Google Guice或Gin我可以指定参数不受依赖注入框架控制:
class SomeEditor {
@Inject
public SomeEditor(SomeClassA a, @Assisted("stage") SomeClassB b) {
}
}
Run Code Online (Sandbox Code Playgroud)
辅助参数stage在SomeEditor创建实例时指定.
SomeClassA的实例取自对象图,SomeClassB的实例在运行时从调用者获取.
在Dagger中有类似的方法吗?
我正在研究一个动态创建HTTP请求的组件,我希望能够模拟这些单元测试请求.
目前实现看起来像这样:
class ModelClass {
public void populate() {
HTTPRequest request = new HTTPRequest();
//configure request...
request.send();
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法使用Guice实例化,request所以我可以用模拟类的实例替换它进行测试?我能想出的最近的是添加一个注入器作为ModelClass的实例变量:
class ModelClass {
private final Injector injector;
ModelClass(Injector injector){
this.injector = injector;
}
public void populate() {
HTTPRequest request = injector.getInstance(HTTPRequest.class);
//configure request...
request.send();
}
}
Run Code Online (Sandbox Code Playgroud)
但这基本上就像使用工厂一样,完全错过了Guice的观点.
注释的类@Singleton必须遵循Singleton设计模式吗?
我的猜测是他们没有:没有必要拥有私有构造函数和static .instance()方法,而是Guice确保只实例化该类的一个实例.
在Guice中,有没有一种方法MethodInterceptor::invoke可以在执行截取的方法之后(而不是紧接在此之前)调用我的实现?
我已将当前代码添加到我的AbstractModule:
bindInterceptor(Matchers.subclassesOf(InterceptedClass.class), Matchers.annotatedWith(MyMethodAnnotation.class), new MyMethodInterceptor());
Run Code Online (Sandbox Code Playgroud) 首先,感谢您为尝试回答该问题所做的任何努力。从当前应用程序注入器(play.api.Play.current.injector),如何获取类的命名实例?我试图将注入器转换为ScalaInjector(net.codingwell.scalaguice.InjectorExtensions.ScalaInjector)和Guice注入器(com.google.inject.Injector),但均未成功。问题在于,只有3种方法可以实例化一个类,所有方法都重载了instanceOf [T]
我使用Guice作为我的依赖注入框架.我想要一些我可以添加的东西,这将使创建REST服务更容易.
我已经看过guice-servlet了,它可以很好地将路径引导到HTTP servlet,但这就是它所做的一切,我希望JAX-RS之类的注释语法可以正常工作,但事实并非如此.
实际上没有使用JAX-RS我已经google了一下,看来Jersey是这个的参考实现,但看起来它使用自己的依赖注入框架并且与Guice不兼容.此外,它有5 + MB的依赖性,这似乎很多我追求的.
Guice是否以这样的方式设计,它不适合JAX-RS,如果是这样,我还应该做些什么呢?
现在我正在阅读Guice的官方文档,但我有一些与Binding Annotation章节相关的问题.
这解释了"带属性的注释".但是,我不确定这个解释.
绑定注释与属性
Guice支持绑定具有属性值的注释.在极少数情况下,您需要这样的注释:
创建注释@interface.创建一个实现注释接口的类.遵循Annotation Javadoc中指定的equals()和hashCode()指南.将此实例传递给annotatedWith()绑定子句.
我不明白这个解释.有什么解释意图?我学习了两个注释,例如@Paypal(在本文档中)和@name.但是,当我想在同一个类中使用两个以上的依赖项时,我们可能无法实现这两个注释?现在我很困惑,有人可以解释一下吗?