我必须从EnvironmentModule触发TestDriver.startPoller()因为 EnvironmentModule 是从 Abstractmodule 扩展的我不确定如何在EnvironmentModule中注入依赖关系是否可能?如果不是,我如何 从EnvironmentModule触发TestDriver.startPoller()
@Log4j2
@AllArgsConstructor(onConstructor = @__(@Inject))
public class TestDriver {
private ClientTrafficCalculator clientTrafficCalculator;
private TaskAllocator taskAllocator;
@SneakyThrows
public void startPoller() {
new Thread(clientTrafficCalculator::prepareRateLimiterForAllClients).start();
new Thread(taskAllocator::processPendingRecordsInDDB).start();
log.info("All threads are started");
}
}
环境模块
@Log4j2
@AllArgsConstructor(onConstructor = @__(@Inject))
public class EnvironmentModule extends AbstractModule {
private TestDriver testDriver;
@SneakyThrows
public EnvironmentModule(final String[] args) {
marketplace = System.getProperty("realm", "USTest");
final String realm = getPropOrDefault("realm", "USTest");
final String root = getPropOrDefault("root", ".");
AppConfig.initialize(SERVICE_NAME, null, appConfigArgs);
TestDriver.startPoller();
}
@Override
public void configure() {
install(new TomcatContainerModule(new ServiceModule(Test)));
}
}
预期的 :
TestDriver.startPoller();
// 这工作正常
当前结果
TestDriver.startPoller();
// thorwing 空指针异常,因为它没有被注入并且TestDriver为空