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);

        SomeService ss = inj.getInstance(SomeService.class);

        // 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?

  • 1
    The Main method is an entry point... It's not code duplication. That's like saying you can only use DatabaseService in a single application, because calling DatabaseService ds = inj.getInstance(DatabaseService.class); more than once would be code duplication. – Robert Harvey Sep 10 '13 at 18:57
  • @RobertHarvey Of course, but I'm wondering if there's some sort of main templating concept or even "main method generator" tool to automatate this boilerplate. – durron597 Sep 10 '13 at 19:31
  • I view that as over-engineering. If you need to start with the same Main method code and then modify it, then copy/paste. It's not a code smell just because ctrl-c and ctrl-v are involved. – Robert Harvey Sep 10 '13 at 19:43
  • @RobertHarvey Fair. I'll give you your 25 rep if you turn that into an answer – durron597 Sep 10 '13 at 19:55
  • 1
    You might want to change your language to something other than Java if such small amounts of boilerplate annoy you ;) – Thomas Eding Sep 10 '13 at 22:11

Nothing about the code example in your question suggests the need for automation. Every Main method is going to be different.

Of course, if you had a database, and you were writing data access code, you might realize that a lot of this code looks almost exactly the same ("CRUD" methods), and you might be tempted to write a code generator that reads your database table schemas and generates classes that correspond to the entities in each table (customers, for example). You would then have the equivalent of an Object-Relational Mapper, arguably a worthy pursuit.

Some programming environments such as Visual Studio have the ability to store code snippets, and even come with a template code generator (T4). But the point of automation is to save time. Consult this chart first, before writing the template:

Is it worth the time?


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.