SpringApplication.run main method

前端 未结 3 1233
自闭症患者
自闭症患者 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:24

    You need to run Application.run() because this method starts whole Spring Framework. Code below integrates your main() with Spring Boot.

    Application.java

    @SpringBootApplication
    public class Application {
    
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }
    

    ReconTool.java

    @Component
    public class ReconTool implements CommandLineRunner {
    
        @Override
        public void run(String... args) throws Exception {
            main(args);
        }
    
        public static void main(String[] args) {
            // Recon Logic
        }
    }
    

    Why not SpringApplication.run(ReconTool.class, args)

    Because this way spring is not fully configured (no component scan etc.). Only bean defined in run() is created (ReconTool).

    Example project: https://github.com/mariuszs/spring-run-magic

提交回复
热议问题