Guice - 绑定由辅助注入工厂创建的实例
Guice - Binding an instance created by assisted injection factory
假设有一个 class 构造函数看起来像这样:
public A(@Assited long id, @Assisten String name, ServiceA serviceA, ServiceB serviceB)
还有一个工厂:
public interface AFactory{
A create(long id, String name);
}
所以要创建 A 的实例,我显然需要做类似的事情:
injector = Guice.createInjector(new MyModule());
AFactory af = injector.getInstance(AFactory .class);
A a = AFactory.create(100, "mike");
但是,
假设我有其他 classes: Class B, Class C 和 Class D 有一个类型为 A 的成员,例如(使用字段注入但可以是 ctor还有):
public class B{
@Inject
A a;
}
并且我希望将 A 的 相同实例 注入那些 classes。
但仍然可以选择将 A 的另一个实例注入其他 classes(假设 Class E 和 F)。
正确的做法是什么?
我只是想不出一个干净的方法来做到这一点。
您可以构造您的模块以使用 Providers(我正在使用 @Provides methods below, but you can use full Provider classes or instances if you'd like), and mark the consistent A as @Singleton. If you want two bindings of A (consistent and inconsistent), at least one of them should be marked with a binding annotation; I'm using @Named
here out of convenience, but you can use any binding annotation as listed in the docs。
public class AModule extends AbstractModule {
@Override public void configure() {
// Install your AFactory module. Here, injections for AFactory should succeed.
install(new FactoryModuleBuilder().build(AFactory.class));
}
/**
* Provides a singleton @Named("consistent") A.
* Inject @Named("consistent") A into B, C, and D; Guice will cache the instance.
*/
@Provides @Singleton @Named("consistent")
A provideConsistentA(AFactory factory) {
return factory.create(100, "mike");
}
/**
* Provides an unscoped A.
* Inject A without an annotation into E and F; each instance will be separate.
*/
@Provides @Singleton A provideUnscopedA(AFactory factory) {
return factory.create(200, "jeff");
}
}
假设有一个 class 构造函数看起来像这样:
public A(@Assited long id, @Assisten String name, ServiceA serviceA, ServiceB serviceB)
还有一个工厂:
public interface AFactory{
A create(long id, String name);
}
所以要创建 A 的实例,我显然需要做类似的事情:
injector = Guice.createInjector(new MyModule());
AFactory af = injector.getInstance(AFactory .class);
A a = AFactory.create(100, "mike");
但是, 假设我有其他 classes: Class B, Class C 和 Class D 有一个类型为 A 的成员,例如(使用字段注入但可以是 ctor还有):
public class B{
@Inject
A a;
}
并且我希望将 A 的 相同实例 注入那些 classes。 但仍然可以选择将 A 的另一个实例注入其他 classes(假设 Class E 和 F)。
正确的做法是什么? 我只是想不出一个干净的方法来做到这一点。
您可以构造您的模块以使用 Providers(我正在使用 @Provides methods below, but you can use full Provider classes or instances if you'd like), and mark the consistent A as @Singleton. If you want two bindings of A (consistent and inconsistent), at least one of them should be marked with a binding annotation; I'm using @Named
here out of convenience, but you can use any binding annotation as listed in the docs。
public class AModule extends AbstractModule {
@Override public void configure() {
// Install your AFactory module. Here, injections for AFactory should succeed.
install(new FactoryModuleBuilder().build(AFactory.class));
}
/**
* Provides a singleton @Named("consistent") A.
* Inject @Named("consistent") A into B, C, and D; Guice will cache the instance.
*/
@Provides @Singleton @Named("consistent")
A provideConsistentA(AFactory factory) {
return factory.create(100, "mike");
}
/**
* Provides an unscoped A.
* Inject A without an annotation into E and F; each instance will be separate.
*/
@Provides @Singleton A provideUnscopedA(AFactory factory) {
return factory.create(200, "jeff");
}
}