我尝试实现'@Named'
注入,但我不想预定义@Named实例(我在最后有一个例子,它实际上正在工作......但不是我想要的方式 - 因为我预定义名字)
提供代码示例比解释我的意思更容易。
public class NamedSingletonExample {
private static Map<String, DoubleProperty> dMap = new HashMap<String, DoubleProperty>();
public static DoubleProperty getNamedDoubleProperty(String name) {
DoubleProperty d = dMap.get(name);
if (d == null) {
d = new SimpleDoubleProperty();
dMap.put(name, d);
}
return d;
}
@Test
public void testNamedSingleton() {
System.out.println("test()");
DoubleProperty d0 = getNamedDoubleProperty("d0");
DoubleProperty d1 = getNamedDoubleProperty("d1");
DoubleProperty d0_2 = getNamedDoubleProperty("d0");
d0.set(1);
d1.set(2);
d0_2.set(4);
System.out.println("expected: false & actual -> " + (d0.get() == d1.get()));
System.out.println("expected: true & actual -> " + (d0.get() == d0_2.get()));
System.out.println("expected: true & actual -> " + (d0 == d0_2));
}
}
这是我期望它使用guice
public class NamedSingletonConsumer {
@Inject
public NamedSingletonConsumer(//
@Named("d0") DoubleProperty d0, //
@Named("d1") DoubleProperty d1, //
@Named("d0_2") DoubleProperty d0_2//
) {
// ...
}
}
我在StackOverflow
上阅读了其他问题的大量内容并使用了谷歌(包括guice
doc),但未能找到相关示例。
现在是“工作”的例子:
public class AllInOneExample {
public AllInOneExample() {
Injector injector = Guice.createInjector(new AllInOneExample.DoublePropertyModule());
DoublePropertyConsumer consumer = injector.getInstance(DoublePropertyConsumer.class);
consumer.test();
}
public static void main(String[] args) {
new AllInOneExample();
}
static class DoublePropertyConsumer {
DoubleProperty d0;
DoubleProperty d1;
DoubleProperty d0_2;
@Inject
public DoublePropertyConsumer(//
@Named("d0") DoubleProperty d0, //
@Named("d1") DoubleProperty d1, //
@Named("d0_2") DoubleProperty d0_2//
) {
this.d0 = d0;
this.d1 = d1;
this.d0_2 = d0_2;
}
public void test() {
System.out.println("test()");
d0.set(1);
d1.set(2);
d0_2.set(4);
System.out.println("expected: false & actual -> " + (d0.get() == d1.get()));
System.out.println("expected: true & actual -> " + (d0.get() == d0_2.get()));
System.out.println("expected: true & actual -> " + (d0 == d0_2));
}
}
static class DoublePropertyFactory {
private Map<String, DoubleProperty> dMap = new HashMap<String, DoubleProperty>();
public DoubleProperty buildProperty(String name) {
DoubleProperty d = dMap.get(name);
if (d == null) {
d = new SimpleDoubleProperty();
dMap.put(name, d);
}
return d;
}
}
static class DoublePropertyModule extends AbstractModule {
@Override
protected void configure() {
DoublePropertyFactory doublePropertyFactory = new DoublePropertyFactory();
requestInjection(doublePropertyFactory);
// bind(DoubleProperty.class).annotatedWith(Named.class).toInstance(doublePropertyFactory .buildProperty(???)));
bind(DoubleProperty.class).annotatedWith(Names.named("d0"))
.toInstance(doublePropertyFactory.buildProperty("d0"));
bind(DoubleProperty.class).annotatedWith(Names.named("d1"))
.toInstance(doublePropertyFactory.buildProperty("d1"));
bind(DoubleProperty.class).annotatedWith(Names.named("d0_2"))
.toInstance(doublePropertyFactory.buildProperty("d0"));
}
}
}