SpringApplication.run main method

前端 未结 3 1244
自闭症患者
自闭症患者 2020-12-24 10:52

I created a project in Eclipse using the Spring Starter project template.

It automatically created an Application class file, and that path matches the path in the P

3条回答
  •  我在风中等你
    2020-12-24 11:27

    Using:

    @ComponentScan
    @EnableAutoConfiguration
    public class Application {
    
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);  
    
            //do your ReconTool stuff
        }
    }
    

    will work in all circumstances. Whether you want to launch the application from the IDE, or the build tool.

    Using maven just use mvn spring-boot:run

    while in gradle it would be gradle bootRun

    An alternative to adding code under the run method, is to have a Spring Bean that implements CommandLineRunner. That would look like:

    @Component
    public class ReconTool implements CommandLineRunner {
    
        @Override
        public void run(String... args) throws Exception {
           //implement your business logic here
        }
    }
    

    Check out this guide from Spring's official guide repository.

    The full Spring Boot documentation can be found here

提交回复
热议问题