Ita*_*man 13 java spring dependency-injection guice
这是我需求的简化版本.
我有一个程序,其中每个B对象都有自己的C和D对象,通过Guice注入.此外,A对象被注入每个C和D对象.
我想要的是:对于每个B对象,它的C和D对象将被注入相同的A对象.
[编辑-开始]
(1)Guice支持"单例"和"原型"模式.但是,我需要的是介于两者之间的东西:我需要A对给定的B对象进行单例WRT(这样注入B对象的C和D将共享一个A对象).对于另一个B对象,我想要另一个A.所以它是一个单例,但对于程序的有限范围(实际上,数据结构的范围有限).
(2)我不介意使用方法(setter)或现场注入的解决方案.
(3)我曾多次尝试实现这一目标,并且总觉得我只需要实现DI容器的一些自定义东西来实现这一点 - 但它从未奏效.因此,我正在寻找一个详细的解决方案(而不仅仅是"挥手")
[编辑-完]
具体来说,我希望程序的输出(如下)为:
Created C0 with [A0]
Created D0 with [A0]
Created B0 with [C0, D0]
Created C1 with [A1]
Created D1 with [A1]
Created B1 with [C1, D1]
Run Code Online (Sandbox Code Playgroud)
它当前产生以下输出的位置:
Created C0 with [A0]
Created D0 with [A1] <-- Should be A0
Created B0 with [C0, D0]
Created C1 with [A2] <-- Should be A1
Created D1 with [A3] <-- Should be A1
Created B1 with [C1, D1]
Run Code Online (Sandbox Code Playgroud)
我期待DI容器允许这种定制,但到目前为止我没有找到解决方案的运气.下面是我的基于Guice的代码,但欢迎使用基于Spring(或其他基于DI容器)的解决方案.
import java.util.Arrays;
import com.google.inject.*;
public class Main {
public static class Super {
private static Map<Class<?>,Integer> map = new HashMap<Class<?>,Integer>();
private Integer value;
public Super(Object... args) {
value = map.get(getClass());
value = value == null ? 0 : ++value;
map.put(getClass(), value);
if(args.length > 0)
System.out.println("Created " + this + " with " + Arrays.toString(args));
}
@Override
public final String toString() {
return "" + getClass().getSimpleName().charAt(0) + value;
}
}
public interface A { }
public static class AImpl extends Super implements A { }
public interface B { }
public static class BImpl extends Super implements B {
@Inject public BImpl(C c, D d) { super(c,d); }
}
public interface C { }
public static class CImpl extends Super implements C {
@Inject public CImpl(A a) { super(a); }
}
public interface D { }
public static class DImpl extends Super implements D {
@Inject public DImpl(A a) { super(a); }
}
public static class MyModule extends AbstractModule {
@Override
protected void configure() {
bind(A.class).to(AImpl.class);
bind(B.class).to(BImpl.class);
bind(C.class).to(CImpl.class);
bind(D.class).to(DImpl.class);
}
}
public static void main(String[] args) {
Injector inj = Guice.createInjector(new MyModule());
inj.getInstance(B.class);
inj.getInstance(B.class);
}
}
Run Code Online (Sandbox Code Playgroud)
小智 13
这是基于原始代码的一种解决方案 - 有三种变化:
这是有效的,因为A的单例绑定现在仅限于每个子注入器.
[注意:
如果您不想继续为B的每个请求创建子模块实例,您可以将子模块实例缓存在主模块的字段中]
import java.util.*;
import com.google.inject.*;
public class Main {
public static class Super {
private static Map<Class<?>,Integer> map = new HashMap<Class<?>,Integer>();
private Integer value;
public Super(Object... args) {
value = map.get(getClass());
value = value == null ? 0 : ++value;
map.put(getClass(), value);
if(args.length > 0)
System.out.println("Created " + this + " with " + Arrays.toString(args));
}
@Override
public final String toString() {
return "" + getClass().getSimpleName().charAt(0) + value;
}
}
public interface A { }
public static class AImpl extends Super implements A { }
public interface B { }
public static class BImpl extends Super implements B {
@Inject public BImpl(C c, D d) { super(c,d); }
}
public interface C { }
public static class CImpl extends Super implements C {
@Inject public CImpl(A a) { super(a); }
}
public interface D { }
public static class DImpl extends Super implements D {
@Inject public DImpl(A a) { super(a); }
}
public static class MyModule extends AbstractModule {
@Override
protected void configure() {}
// >>>>>>>>
@Provides
B builder( Injector injector ) {
return injector.createChildInjector( new SubModule() ).getInstance( BImpl.class );
}
// <<<<<<<<
}
// >>>>>>>>
public static class SubModule extends AbstractModule {
@Override
protected void configure() {
bind(A.class).to(AImpl.class).in( Scopes.SINGLETON );
bind(C.class).to(CImpl.class);
bind(D.class).to(DImpl.class);
}
}
// <<<<<<<<
public static void main(String[] args) {
Injector inj = Guice.createInjector(new MyModule());
inj.getInstance(B.class);
inj.getInstance(B.class);
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2663 次 |
最近记录: |