简体   繁体   English

从标准 Java 应用程序运行 Spring 启动 jar

[英]Run Spring Boot jar from standard Java application

I have a large application with a database, Swing UI etc. Now I want to add a REST API for this application.我有一个带有数据库的大型应用程序,Swing UI 等。现在我想为这个应用程序添加一个 REST API。 Spring Boot allows easy generation of a REST API with useful features such as OpenApi documentation and authentication. Spring Boot 允许轻松生成 REST API 以及 OpenApi 文档和身份验证等有用功能。

However when I run the Spring Boot application from within the large non Spring Boot application the Spring Boot application gets confused by the dependencies of the parent application and fails to run.但是,当我从大型非 Spring 引导应用程序中运行 Spring 引导应用程序时,Spring 引导应用程序会被父应用程序的依赖项混淆并且无法运行。

So my requirement is this: run a Spring Boot application from withing a non Spring Boot application without dependency interference from the parent application.所以我的要求是:从非 Spring 引导应用程序运行 Spring 引导应用程序,而不受父应用程序的依赖干扰。 I am currently running the Spring Boot application by adding the executable jar as a dependency and then calling org.springframework.boot.loader.JarLauncher.main(new String[0]);我目前正在运行 Spring 引导应用程序,方法是添加可执行文件 jar 作为依赖项,然后调用org.springframework.boot.loader.JarLauncher.main(new String[0]); to run the Spring Boot application.运行 Spring 引导应用程序。 I am not set on this way of doing it and any suggestions will be greatly appreciated.我不打算这样做,任何建议都将不胜感激。

Spring is a huge collection of highly configurable software libraries that can be used to setup (among other things) REST API endpoints and OpenAPI documentation and UI. Spring 是大量高度可配置的软件库,可用于设置(除其他外)REST API 端点和 OpenAPI 文档和 UI。

Spring Boot is a project to simplify the process of using these libraries by applying an opinionated view of how to run them within a standalone process. Spring Boot 是一个项目,通过应用有关如何在独立进程中运行它们的固执观点来简化使用这些库的过程。

By asking how to run a Spring Boot application within a larger application you are trying to get the benefit of the opinionated setup while violating the assumptions that the setup is based on.通过询问如何在更大的应用程序中运行 Spring 引导应用程序,您试图在违反设置所基于的假设的同时获得自以为是的设置的好处。 I guess in theory it might be possible using some sort of handrolled classloader isolation, but once you've solved the dependency problem you'll probably end up with class version conflicts, issues with configuration locations, etc. In short if it is possible at all the effort of doing so would negate the benefit.我想理论上它可能会使用某种手动类加载器隔离,但是一旦你解决了依赖问题,你可能最终会遇到 class 版本冲突、配置位置问题等。简而言之,如果它可能在这样做的所有努力都会抵消好处。

There are two ways of resolving the issue.有两种解决问题的方法。

  1. Use Spring Boot to build your API as a standalone process.使用 Spring Boot 将 API 构建为独立进程。 Configure the new process to talk to the same database as the existing application.将新进程配置为与现有应用程序相同的数据库进行通信。 If neccessary factor out any code common to both the existing application and the API (JPA entities, DAO classes etc) into a shared library.如果需要,将现有应用程序和 API(JPA 实体、DAO 类等)共同的任何代码分解到共享库中。 If you go with this option you will have the overhead of having to manage multiple kinds of process in your production environment, which is more complex - but has advantages in terms of decoupling scaling, release cycles, restart times.如果您使用此选项 go,您将不得不在生产环境中管理多种过程的开销,这更复杂 - 但在解耦缩放、发布周期、重启时间方面具有优势。 See the debate on microservices ( https://martinfowler.com/articles/microservices.html ).请参阅关于微服务的辩论 ( https://martinfowler.com/articles/microservices.html )。

  2. Use the Spring libraries that provide REST and OpenAPI features as part of your existing application, without using Spring Boot.使用提供 REST 和 OpenAPI 功能的 Spring 库作为现有应用程序的一部分,而不使用 Spring 引导。 You'll need to have SpringMVC set up in order to use @RestController annotated classes.您需要设置 SpringMVC 才能使用 @RestController 注释类。 If your existing application is a web application that's not too bad ( https://docs.spring.io/spring-framework/docs/3.2.x/spring-framework-reference/html/mvc.html ). If your existing application is a web application that's not too bad ( https://docs.spring.io/spring-framework/docs/3.2.x/spring-framework-reference/html/mvc.html ). If it's not run in a webserver already you'll have to launch the SpringMVC framework in an embedded webserver ( https://devcenter.heroku.com/articles/create-a-java-web-application-using-embedded-tomcat ).如果它尚未在网络服务器中运行,则必须在嵌入式网络服务器中启动 SpringMVC 框架( https://devcenter.heroku.com/articles/create-a-java-web-application-using-embedded-tomcat ) . There's a good article on adding OpenAPI to an MVC application here: https://www.baeldung.com/spring-rest-openapi-documentation .这里有一篇关于将 OpenAPI 添加到 MVC 应用程序的好文章: https://www.baeldung.com/spring-rest-openapi-documentation

you can simply exclude auto-configure dependencies.您可以简单地排除自动配置依赖项。 Here is an informative link https://www.marcobehler.com/guides/spring-boot这是一个信息链接https://www.marcobehler.com/guides/spring-boot

Here is a code snippet of how to exclude when applications get started.以下是应用程序启动时如何排除的代码片段。

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class)
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(PayPalApplication.class, args);
    }
}

or the other ways或其他方式

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration

I want to share another Spring Framework dependencies https://www.marcobehler.com/guides/spring-framework我想分享另一个 Spring 框架依赖https://www.marcobehler.com/guides/spring-framework

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 java -jar 运行 Spring Boot 应用程序时未提交事务 - Transaction is not committed when spring boot application is run from java -jar 无法从命令提示符将 Spring 启动应用程序作为可运行的 jar 运行 - Not able run Spring boot application as runnable jar from command prompt Spring Boot应用程序 - 使用“mvn spring-boot:run”和“java -jar”启动时间的差异 - Spring Boot application - difference in startup time with “mvn spring-boot:run” and “java -jar” Spring 启动应用程序 jar 无法运行 - Spring Boot Application jar failed to run 我可以从另一个 spring boot 应用程序中编译并运行 spring boot jar 吗? - Can I compile and run a spring boot jar from inside another spring boot application? 使用Java -jar运行Spring Boot - run Spring Boot using Java -jar 如何从 Spring Boot 应用程序的文件夹作为 jar 运行主应用程序(不是 Spring Boots 应用程序) - How to run a main application (not a spring boots appl) from a folder of Spring Boot application as jar 构建 jar 并运行 spring 从 cmd 启动 - Build jar and run spring boot from cmd 使用java.jar以tools.jar作为依赖项运行spring boot - run spring boot with java -jar with tools.jar as dependency 从 Spring 启动应用程序启动 jar 应用程序 - Start jar app from Spring Boot application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM