Guice has the concept of a Scope, commonly used for a web server request.
Are Scopes viable for use in a long-lived object, whose lifecycle may be asynchronous compared to the running code?
E.g. Sponge & Bukkit both provide events that fire when a world is loaded / unloaded.
These worlds are then ticked, and various events thrown.
Is it worthwhile using Scopes and Scoping annotations in order to have an annotation that takes a parameter, in order to inject / initialize services / world specific listeners whenever a world is loaded and unloaded?
public abstract class WorldService {
World world;
public WorldService(World world) {
this.world = world;
}
abstract void tick();
abstract void start();
abstract void stop();
public static class TimeTracker extends WorldService {
private final Logger logger;
private int count;
@Inject
public TimeTracker(World world, Logger logger) {
super(world);
this.logger = logger;
}
@Override
void tick() {
count++;
}
@Override
void start() {
count = 0;
}
@Override
void stop() {
logger.debug("World loaded for "+count+" ticks");
}
}
}