Spring Boot - How to specify an alternate start-class? (Multiple Entry Points)

前端 未结 3 1953
既然无缘
既然无缘 2021-02-04 04:47

I want to add an alternate entry point to my Spring-Boot application. I would prefer to keep this as a fat jar. Is this possible?

According to their documentation, the

3条回答
  •  误落风尘
    2021-02-04 05:10

    I took a different approach and use a command line parameter to determine which class to use as my SpringApplication class. I only have a single main() method, but different Application classes with different configurations that are used based on a command line param.

    I have a single class with a main() in it:

    public static void main(String[] args) {
        SpringApplication app;
        if( ArrayUtils.contains(args, "--createdb")){
            app = new SpringApplication(CreateDB.class);
            args = (String[])ArrayUtils.add(args, "--spring.jpa.hibernate.ddl-auto=create");
        } else {
            app = new SpringApplication(Application.class);
        }
    
        app.setWebEnvironment(false);
        app.setShowBanner(false);
        app.addListeners(new ConfigurationLogger());
    
        // launch the app
        ConfigurableApplicationContext context = app.run(args);
    
        // finished so close the context
        context.close();
    }
    

    But I have 2 different SpringApplication classes: Application.class & CreateDB.class. Each class defines a different @ComponentScan path as well as different @EnableAutoConfiguration options and different @Configuration options. Finally, based on my command line arguments, I can decide whether to programatically enable additional profiles/etc.

    In my case, I want a different launcher to just create the DB schema and exit, so I've forced the command line parameter.

提交回复
热议问题