无法使用 Guice 和 Vertx 将同一实例注入多个 Verticles
Cant Inject same instance to multiple Verticles using Guice and Vertx to
我有 3 个顶点。
我创建了 class AImpl,它实现了 class A,如下所示
@Singleton
public class AImpl implements A {
public LocationServiceImpl() {
System.out.println("initiated once");
}
public void doSomething(){..}
顶点 1 如下所示:
public class MyVerticle1 extends AbstractVerticle {
...
@Inject
private A a;
@Override
public void start(Future<Void> fut) {
Guice.createInjector(new AppInjector()).injectMembers(this);
a.doSomething(..);
..}
MyVerticle2 和 MyVerticle3 看起来一样。
引导码:
public class AppInjector extends AbstractModule {
public AppInjector() {
}
@Override
protected void configure() {
bind(A.class).to(AImpl.class).in(Singleton.class);
}
现在,当我 运行 vertx 时,我可以看到我得到了 3 个不同的 AImpl 实例:
public static void main(String[] args) throws InterruptedException {
final Logger logger = Logger.getLogger(StarterVerticle.class);
ClusterManager mgr = new HazelcastClusterManager();
VertxOptions options = new VertxOptions().setClusterManager(mgr);
Vertx.clusteredVertx(options, res -> {
if (res.succeeded()) {
Vertx vertx = res.result();
vertx.deployVerticle(new MyVerticle1());
vertx.deployVerticle(new MyVerticle2());
vertx.deployVerticle(new MyVerticle3());
logger.info("Vertx cluster started!");
} else {
logger.error("Error initiating Vertx cluster");
}
});
控制台:
2015-09-15 16:36:15,611 [vert.x-eventloop-thread-0] INFO - Vertx cluster started!
initiated once
initiated once
initiated once
我在用 guice 滥用什么?为什么我没有得到相同的 AImpl 实例?
谢谢,
雷.
您使用 guice 的方式有误。您正在通过 new 创建 MyVerticle 实例,并在其启动消息中创建注入器。因此你最终得到 3 个注入器,每个注入器都有一个单例。
您必须在 main() 方法中创建一次注入器,然后让 guice 处理 MyVerticles 的创建:
Injector injector = Guice.createInjector(....);
...
vertx.deployVerticle(injector.getInstance(MyVerticle1.class);
现在注入器只为 AImpl 创建一个实例并将其重复用于所有 @Inject AImpl
位置。从您的启动方法中完全删除注入器。
使用 guice 时的 2 条经验法则:
- 避免使用
new
- 尝试在您的 main() 方法中只使用一个注入器
我有 3 个顶点。
我创建了 class AImpl,它实现了 class A,如下所示
@Singleton
public class AImpl implements A {
public LocationServiceImpl() {
System.out.println("initiated once");
}
public void doSomething(){..}
顶点 1 如下所示:
public class MyVerticle1 extends AbstractVerticle {
...
@Inject
private A a;
@Override
public void start(Future<Void> fut) {
Guice.createInjector(new AppInjector()).injectMembers(this);
a.doSomething(..);
..}
MyVerticle2 和 MyVerticle3 看起来一样。
引导码:
public class AppInjector extends AbstractModule {
public AppInjector() {
}
@Override
protected void configure() {
bind(A.class).to(AImpl.class).in(Singleton.class);
}
现在,当我 运行 vertx 时,我可以看到我得到了 3 个不同的 AImpl 实例:
public static void main(String[] args) throws InterruptedException {
final Logger logger = Logger.getLogger(StarterVerticle.class);
ClusterManager mgr = new HazelcastClusterManager();
VertxOptions options = new VertxOptions().setClusterManager(mgr);
Vertx.clusteredVertx(options, res -> {
if (res.succeeded()) {
Vertx vertx = res.result();
vertx.deployVerticle(new MyVerticle1());
vertx.deployVerticle(new MyVerticle2());
vertx.deployVerticle(new MyVerticle3());
logger.info("Vertx cluster started!");
} else {
logger.error("Error initiating Vertx cluster");
}
});
控制台:
2015-09-15 16:36:15,611 [vert.x-eventloop-thread-0] INFO - Vertx cluster started!
initiated once
initiated once
initiated once
我在用 guice 滥用什么?为什么我没有得到相同的 AImpl 实例?
谢谢, 雷.
您使用 guice 的方式有误。您正在通过 new 创建 MyVerticle 实例,并在其启动消息中创建注入器。因此你最终得到 3 个注入器,每个注入器都有一个单例。
您必须在 main() 方法中创建一次注入器,然后让 guice 处理 MyVerticles 的创建:
Injector injector = Guice.createInjector(....);
...
vertx.deployVerticle(injector.getInstance(MyVerticle1.class);
现在注入器只为 AImpl 创建一个实例并将其重复用于所有 @Inject AImpl
位置。从您的启动方法中完全删除注入器。
使用 guice 时的 2 条经验法则:
- 避免使用
new
- 尝试在您的 main() 方法中只使用一个注入器