运行 Spring 从标准 Java 应用程序启动 jar
Run Spring Boot jar from standard Java application
我有一个带有数据库、Swing UI 等的大型应用程序。现在我想为这个应用程序添加一个 REST API。 Spring Boot 允许轻松生成具有 OpenApi 文档和身份验证等有用功能的 REST API。
但是,当我从大型非 Spring 引导应用程序中 运行 Spring 引导应用程序时,Spring 引导应用程序会被父应用程序的依赖项弄糊涂并且未能 运行.
所以我的要求是:运行 Spring 从非 Spring 启动应用程序启动应用程序,不受父应用程序的依赖性干扰。我目前 运行 通过将可执行 jar 添加为依赖项然后调用 Spring 引导应用程序
org.springframework.boot.loader.JarLauncher.main(new String[0]);
到 运行 Spring 引导应用程序。我不喜欢这样做,任何建议将不胜感激。
Spring 是一个高度可配置的软件库的巨大集合,可用于设置(除其他外)REST API 端点和 OpenAPI 文档和 UI.
Spring Boot 是一个项目,通过应用关于如何在独立进程中 运行 它们的自以为是的观点来简化使用这些库的过程。
通过询问如何在更大的应用程序中 运行 Spring 引导应用程序,您试图在违反设置所基于的假设的同时获得固执己见的设置的好处。我猜理论上可能会使用某种手动 class 加载器隔离,但是一旦你解决了依赖性问题,你可能会遇到 class 版本冲突、配置位置问题,等等。简而言之,如果可能的话,这样做的努力将抵消好处。
有两种方法可以解决这个问题。
使用 Spring 引导将您的 API 构建为独立进程。配置新进程以与现有应用程序相同的数据库进行对话。如果有必要,将现有应用程序和 API(JPA 实体、DAO classes 等)共有的任何代码分解到共享库中。如果你选择这个选项,你将不得不在你的生产环境中管理多种流程,这更复杂——但在解耦扩展、发布周期、重启时间方面有优势。查看关于微服务的辩论 (https://martinfowler.com/articles/microservices.html)。
使用提供 REST 和 OpenAPI 功能的 Spring 库作为现有应用程序的一部分,而不使用 Spring Boot。您需要设置 SpringMVC 才能使用带注释的 @RestController classes。如果您现有的应用程序是一个还不错的 Web 应用程序 (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.
您可以简单地排除自动配置依赖项。这里有一个信息linkhttps://www.marcobehler.com/guides/spring-boot
这是一个代码片段,说明如何在应用程序启动时排除。
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class)
public class Application {
public static void main(String[] args) {
SpringApplication.run(PayPalApplication.class, args);
}
}
或其他方式
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
我想分享另一个 Spring 框架依赖项 https://www.marcobehler.com/guides/spring-framework
我有一个带有数据库、Swing UI 等的大型应用程序。现在我想为这个应用程序添加一个 REST API。 Spring Boot 允许轻松生成具有 OpenApi 文档和身份验证等有用功能的 REST API。
但是,当我从大型非 Spring 引导应用程序中 运行 Spring 引导应用程序时,Spring 引导应用程序会被父应用程序的依赖项弄糊涂并且未能 运行.
所以我的要求是:运行 Spring 从非 Spring 启动应用程序启动应用程序,不受父应用程序的依赖性干扰。我目前 运行 通过将可执行 jar 添加为依赖项然后调用 Spring 引导应用程序
org.springframework.boot.loader.JarLauncher.main(new String[0]);
到 运行 Spring 引导应用程序。我不喜欢这样做,任何建议将不胜感激。
Spring 是一个高度可配置的软件库的巨大集合,可用于设置(除其他外)REST API 端点和 OpenAPI 文档和 UI.
Spring Boot 是一个项目,通过应用关于如何在独立进程中 运行 它们的自以为是的观点来简化使用这些库的过程。
通过询问如何在更大的应用程序中 运行 Spring 引导应用程序,您试图在违反设置所基于的假设的同时获得固执己见的设置的好处。我猜理论上可能会使用某种手动 class 加载器隔离,但是一旦你解决了依赖性问题,你可能会遇到 class 版本冲突、配置位置问题,等等。简而言之,如果可能的话,这样做的努力将抵消好处。
有两种方法可以解决这个问题。
使用 Spring 引导将您的 API 构建为独立进程。配置新进程以与现有应用程序相同的数据库进行对话。如果有必要,将现有应用程序和 API(JPA 实体、DAO classes 等)共有的任何代码分解到共享库中。如果你选择这个选项,你将不得不在你的生产环境中管理多种流程,这更复杂——但在解耦扩展、发布周期、重启时间方面有优势。查看关于微服务的辩论 (https://martinfowler.com/articles/microservices.html)。
使用提供 REST 和 OpenAPI 功能的 Spring 库作为现有应用程序的一部分,而不使用 Spring Boot。您需要设置 SpringMVC 才能使用带注释的 @RestController classes。如果您现有的应用程序是一个还不错的 Web 应用程序 (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.
您可以简单地排除自动配置依赖项。这里有一个信息linkhttps://www.marcobehler.com/guides/spring-boot
这是一个代码片段,说明如何在应用程序启动时排除。
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class)
public class Application {
public static void main(String[] args) {
SpringApplication.run(PayPalApplication.class, args);
}
}
或其他方式
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
我想分享另一个 Spring 框架依赖项 https://www.marcobehler.com/guides/spring-framework