如何在没有嵌入式 tomcat 的情况下启动 Spring Boot 应用程序? [英] How do I launch a Spring Boot application without the embedded tomcat?

查看:46
本文介绍了如何在没有嵌入式 tomcat 的情况下启动 Spring Boot 应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 jhipster 生成的 Spring Boot 应用程序,它运行良好.但是,我还需要为一些后台批处理作业创建第二个应用程序,并且这个应用程序使用了第一个应用程序的大部分 spring 服务.我所做的是创建第二个主类,它启动一个 spring 启动应用程序.问题是这也会启动嵌入式 Web 服务器和所有仅对 Web 应用程序有用的服务.我只需要没有专门绑定到 GUI 的服务、持久性和其他类.

I have a Spring Boot application which was generated using jhipster, and it works fine. However, I also need to create a second application for some back-office batch jobs, and this application uses most of the spring services of the first application. What I did is create a second main class, which starts a spring boot application. The problem is this also starts the embedded web-server and all the services that are only useful for the web app. I only need the services, persistence and other classes that are not specifically tied to the GUI.

这是我的两个主要类(简化)

Here are my two main classes (simplified)

普通的 spring-boot 应用程序:

The normal spring-boot app:

@ComponentScan
@AutoConfigure
class Application {
    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(Application.class)
        app.run(args)
    }
}

后台应用:

@ComponentScan
@AutoConfigure
class BackOfficeApplication {
    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(BackOfficeApplication.class)
        app.run(args)
    }
}

什么有效:我的后台应用程序可以访问我需要的一切.Spring 服务、bean 等问题:后台应用程序启动 GUI,阻止我同时启动它两次.

What works: My back office application has access to everything that I need. Spring services, beans, etc. The problem: The back office app starts the GUI, preventing me to launch it twice at the same time.

有没有办法关闭嵌入式tomcat服务器的启动?否则,有没有办法以另一种不会启动嵌入式服务器的方式加载 spring 应用程序上下文?

Is there a way to deactivate the launching of the embedded tomcat server? Otherwise, is there a way to load the spring application context in another way that wouldn't start the embedded server ?

一些细节:* 我不使用 mvn spring-boot:run 启动应用程序.我直接用java(或使用eclipse

Some details: * I don't start the app by using mvn spring-boot:run. I launch the class directly with java (or using eclipse

推荐答案

SpringApplication 有一个属性 webEnvironment.如果 Tomcat 在类路径上,则默认为 true,但您可以将其设置为 false(以编程方式或使用 spring.main.webEnvironment).

SpringApplication has a property webEnvironment. It defaults to true if Tomcat is on the classpath but you can set it to false (programmatically or with spring.main.webEnvironment).

这篇关于如何在没有嵌入式 tomcat 的情况下启动 Spring Boot 应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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