SpringApplication.run主要方法 [英] SpringApplication.run main method
问题描述
我使用Spring Starter项目模板在Eclipse中创建了一个项目。
I created a project in Eclipse using the Spring Starter project template.
它自动创建了一个Application类文件,该路径与POM.xml中的路径匹配文件,所以一切都很好。这是Application类:
It automatically created an Application class file, and that path matches the path in the POM.xml file, so all is well. Here is the Application class:
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
//SpringApplication.run(ReconTool.class, args);
ReconTool.main(args);
}
}
这是我正在构建的命令行应用程序为了让它运行我必须注释掉SpringApplication.run行,只需从我的其他类中添加main方法即可运行。
除了这个快速的jerry-rig之外,我可以使用Maven构建它,并且它作为Spring应用程序运行,类似于。
This is a command line app that I am building and in order to get it to run I had to comment out the SpringApplication.run line and just add the main method from my other class to run. Other than this quick jerry-rig, I can build it using Maven and it runs as a Spring application, sort of.
我宁愿,但是,不必注释掉那一行,并使用完整的Spring框架。我该怎么做?
I'd rather, however, not have to comment out that line, and use the full Spring framework. How can I do this?
推荐答案
你需要运行 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
这篇关于SpringApplication.run主要方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!