Spring: Run multiple “SpringApplication.Run()” in application main method

我与影子孤独终老i 提交于 2019-12-22 07:59:27

问题


Hey I am new to spring and am trying to run multiple run methods in the main in my Applications.java.

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?

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

回答1:


I think you try to run one spring application with two configurations.

The normal way to do this is to use the @Import annotation.

@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




回答2:


the run method supports an array of Objects. This code works

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



回答3:


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.



来源:https://stackoverflow.com/questions/24516070/spring-run-multiple-springapplication-run-in-application-main-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!