@MainArgs
Both SpincastCoreGuiceModule and SpincastDefaultGuiceModule
Guice modules have a constructor which accepts
String[] mainArgs. You can pass to it the arguments received in
your main(...) method. For example:
public static void main(String[] args) {
Injector guice = Guice.createInjector(new SpincastDefaultGuiceModule(args));
App app = guice.getInstance(App.class);
app.start();
}
By doing so, those arguments will be bound, using a @MainArgs
annotation. You can then inject them anywhere you need:
public class AppConfig extends SpincastConfig implements IAppConfig {
private final String[] mainArgs;
@Inject
public AppConfig(@MainArgs String[] mainArgs) {
this.mainArgs = mainArgs;
}
protected String[] getMainArgs() {
return this.mainArgs;
}
@Override
public int getHttpServerPort() {
int port = super.getHttpServerPort();
if(getMainArgs().length > 0) {
port = Integer.parseInt(getMainArgs()[0]);
}
return port;
}
}