Spring:运行多个“SpringApplication.Run()”在应用主要方法 [英] Spring: Run multiple "SpringApplication.Run()" in application main method

查看:526
本文介绍了Spring:运行多个“SpringApplication.Run()”在应用主要方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我是Spring的新手,我正在尝试在Applications.java的main中运行多个run方法。

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

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


推荐答案

我认为您尝试使用两种配置运行一个spring应用程序。

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

通常的方法是使用 @Import 注释。

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 春天参考章节4.12.5编写基于Java的配置
使用@Import注释

这篇关于Spring:运行多个“SpringApplication.Run()”在应用主要方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆