简体   繁体   中英

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. Spring Boot allows easy generation of a REST API with useful features such as OpenApi documentation and authentication.

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.

So my requirement is this: run a Spring Boot application from withing a non Spring Boot application without dependency interference from the parent application. 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]); to run the Spring Boot application. 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 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.

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

There are two ways of resolving the issue.

  1. Use Spring Boot to build your API as a standalone process. 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. 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. See the debate on microservices ( 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. You'll need to have SpringMVC set up in order to use @RestController annotated classes. 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 ). There's a good article on adding OpenAPI to an MVC application here: 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM