Java Spring:在应用程序主方法中运行多个“SpringApplication.Run()”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24516070/
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
Spring: Run multiple "SpringApplication.Run()" in application main method
提问by user3073234
Hey I am new to spring and am trying to run multiple run methods in the main in my Applications.java.
嘿,我是 spring 的新手,我试图在我的 Applications.java 的 main 中运行多个 run 方法。
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.context.annotation.ComponentScan;
@ComponentScan
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
SpringApplication.run(ScheduledTasks.class);
}
}
When I try to run this I get an exception.
当我尝试运行它时,出现异常。
Is there a way to call both run methods in the main?
有没有办法在 main 中调用两个 run 方法?
-StackTrace
-堆栈跟踪
org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.boot.context.embedded.EmbeddedServletContainerException: Unable to start embedded Tomcat
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:135)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:476)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:120)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:683)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:313)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:944)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:933)
at testWebApp.Application.main(Application.java:13)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:53)
采纳答案by Ralph
I think you try to run one spring application with two configurations.
我认为您尝试使用两种配置运行一个 spring 应用程序。
The normal way to do this is to use the @Import
annotation.
执行此操作的正常方法是使用@Import
注释。
@ComponentScan
@EnableAutoConfiguration
@Import(ScheduledTasks.class)
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
@See Spring Reference Chapter 4.12.5 Composing Java-based configurations Using the @Import annotation
回答by Dave Syer
Without seeing the code it's not possible to say, but my guess is you are trying to launch 2 webapps with the same port. You can change the port of one if them, or make it a non webapp. I recommend using SpringApplicationBuilder
to set those things up.
没有看到代码就不可能说,但我的猜测是您正在尝试使用相同的端口启动 2 个 web 应用程序。您可以更改其中一个的端口,或者使其成为非 Web 应用程序。我建议使用SpringApplicationBuilder
来设置这些东西。
回答by user3073234
the run method supports an array of Objects. This code works
run 方法支持对象数组。此代码有效
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.context.annotation.ComponentScan;
@ComponentScan
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
Object[] sources = {Application.class, ScheduledTasks.class};
SpringApplication.run(sources, args);
}
}