Now that I've gotten into a dependency injection groove, I find main methods for different applications all look basically the same. This is similar to stuff you might find on the Guice documentation, but I'll put an example here:
public class MyApp {
public static void main(String... args) {
Injector inj = Guice.createInjector(new SomeModule(),
new DatabaseModule()
new ThirdModule());
DatabaseService ds = inj.getInstance(DatabaseService.class);
ds.start();
SomeService ss = inj.getInstance(SomeService.class);
ss.start();
// etc.
}
}
For multiple applications I have main methods that all look just like that. But now, lets say I want to make a brand new app, that, say, reuses my DatabaseModule
. I pretty much have to copy and paste the main
method and make appropriate changes... I view copy-paste as a code smell. Further, lets say I realize I should probably be putting shutdown hooks in, now I have to go through and change every main method in all my applications to attach shutdown hooks to each service.
Is there some good way to template this process and minimize the boilerplate?