Java SpringApplication.run 主要方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24271705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SpringApplication.run main method
提问by Alexander Mills
I created a project in Eclipse using the Spring Starter project template.
我使用 Spring Starter 项目模板在 Eclipse 中创建了一个项目。
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:
它自动创建了一个 Application 类文件,该路径与 POM.xml 文件中的路径匹配,所以一切正常。这是应用程序类:
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
//SpringApplication.run(ReconTool.class, args);
ReconTool.main(args);
}
}
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.
这是我正在构建的命令行应用程序,为了让它运行,我必须注释掉 SpringApplication.run 行,只需添加其他类中的 main 方法即可运行。除了这个快速的 jerry-rig 之外,我可以使用 Maven 构建它并且它作为 Spring 应用程序运行,有点。
I'd rather, however, not have to comment out that line, and use the full Spring framework. How can I do this?
但是,我宁愿不必注释掉该行,而是使用完整的 Spring 框架。我怎样才能做到这一点?
采纳答案by MariuszS
You need to run Application.run()
because this method starts whole Spring Framework. Code below integrates your main()
with Spring Boot.
您需要运行,Application.run()
因为此方法启动整个 Spring Framework。下面的代码将您main()
与 Spring Boot集成。
Application.java
Application.java
@SpringBootApplication
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
}
}
Why not SpringApplication.run(ReconTool.class, args)
为什么不 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).
因为这种方式弹簧没有完全配置(没有组件扫描等)。仅创建在 run() 中定义的 bean (ReconTool)。
Example project: https://github.com/mariuszs/spring-run-magic
回答by geoand
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.
将在所有情况下工作。无论您是想从 IDE 还是构建工具启动应用程序。
Using maven just use mvn spring-boot:run
使用 maven 只需使用 mvn spring-boot:run
while in gradle it would be gradle bootRun
而在 gradle 中,它会是 gradle bootRun
An alternative to adding code under the run method, is to have a Spring Bean that implements CommandLineRunner
. That would look like:
在 run 方法下添加代码的另一种方法是使用一个实现CommandLineRunner
. 那看起来像:
@Component
public class ReconTool implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
//implement your business logic here
}
}
Check out thisguide from Spring's official guide repository.
从 Spring 的官方指南存储库中查看本指南。
The full Spring Boot documentation can be found here
完整的 Spring Boot 文档可以在这里找到
回答by RamV
One more way is to extend the application (as my application was to inherit and customize the parent). It invokes the parent and its commandlinerunner automatically.
另一种方法是扩展应用程序(因为我的应用程序是继承和自定义父级)。它会自动调用父级及其 commandlinerunner。
@SpringBootApplication
public class ChildApplication extends ParentApplication{
public static void main(String[] args) {
SpringApplication.run(ChildApplication.class, args);
}
}