Partially inspired by a question/some work by Leon Pennings (on this
list), partially because of my interest in Guice - if been trying to
test some stuff on how best to integrate Guice and T5. Before asking
whether my approach is appropriate I would like to ask what your opinion
is on the whole idea of mixing Guice and T5 IoC?
This is what I've done for the NumberGuess tutorial.
public interface IPlayGenerator {
int generateRandomNumber();
}
public class PlayGeneratorImpl implements IPlayGenerator {
public int generateRandomNumber() {
Random random = new Random();
return random.nextInt(10) + 1;
}
}
//Guice stuff
public class PlayModule extends AbstractModule {
@Override
protected void configure() {
bind(IPlayGenerator.class).to(PlayGeneratorImpl.class).in(Scopes.SINGLETON);
}
}
//Actual client service
public class NumberGuessPlayer {
private final IPlayGenerator generator;
//Guice Inject
@Inject
public NumberGuessPlayer(IPlayGenerator generator) {
this.generator = generator;
}
public int play() {
return generator.generateRandomNumber();
}
}
//T5/Guice bootstrapping
public class AppModule {
// Guice Injector instance
private static final Injector injector;
static {
injector = Guice.createInjector(new Module[] { new PlayModule() });
}
public static NumberGuessPlayer buildNumberGuessPlayerFinder() {
return injector.getInstance(NumberGuessPlayer.class);
}
...
...
}
//Start page
public class Start {
@InjectPage
private Guess guess;
/T5 Inject
@Inject
private NumberGuessPlayer player;
Object onAction() {
int target = player.play();
guess.setup(target);
return guess;
}
}
Any comments, tips....?
-J
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]