且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

SpringApplication.run主要方法

更新时间:2023-11-18 15:45:34

你需要运行 Application.run()因为这个方法启动整个Spring Framework。下面的代码将您的 main()与Spring Boot集成。

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

@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application {

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



ReconTool.java



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
    }
}



为什么不 SpringApplication.run(ReconTool.class,args)



因为这样弹簧没有完全配置(没有组件扫描等)。只创建run()中定义的bean(ReconTool)。

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

示例项目: https://github.com/mariuszs/spring-run-magic