简体   繁体   English

如何在Spring Boot REST应用程序中重用类?

[英]How to reuse classes in a Spring Boot REST application?

I have a Spring Boot REST application with JPA entities and Repository classes (and related services) that works very well. 我有一个带有JPA实体和Repository类(及相关服务)的Spring Boot REST应用程序,效果很好。 Now I would like to reuse these classes for other purposes, like weekly CRON jobs and similar one-time processes which will be run from the command line. 现在,我想将这些类用于其他目的,例如每周的CRON作业和类似的一次性过程,这些过程将从命令行运行。

What would be the best way to do this? 最好的方法是什么? The challenge is that the persistence context properties are set in application.properties, and the persistence context isn't initialized unless the Application class is initialized. 挑战在于,持久性上下文属性是在application.properties中设置的,除非初始化Application类,否则不会初始化持久性上下文。

I can break out all of these classes into a separate project, and use a different way to define the persistence context there, but this becomes more of a maintenance headache if anything changes with the entities or DAO methods. 我可以将所有这些类分解到一个单独的项目中,并使用不同的方式在其中定义持久性上下文,但是如果实体或DAO方法发生任何更改,这将变得更加麻烦。

What I would really like is to have a way, from the command line, to tell Spring Boot to run another class instead of the main Application (and have the persistence context properly initialized). 我真正想要的是从命令行中有一种方法,可以告诉Spring Boot运行另一个类而不是主Application(并正确初始化持久性上下文)。 Any way to do this? 有什么办法吗?

(Note I asked a similar question which got no response: Possible to use Spring Boot repositories from another main class? ) (请注意,我问了一个类似的问题,但没有得到答复: 可以使用另一个主类的Spring Boot存储库吗?

[Edit] is it possible to do this by creating a @component that implements the CommandLineRunner? [编辑]是否可以通过创建实现CommandLineRunner的@component来做到这一点? I just want it to run a simple one-time process and not the full REST application. 我只希望它运行一个简单的一次性过程而不是完整的REST应用程序。

There are a number of ways you could do this. 您可以通过多种方法来执行此操作。 You can have multiple Main classes, and then select which application yuo want to start select main class , however if you don't know how ComponetScan works you will end up loading both applications if you are not careful. 您可以具有多个Main类,然后选择要启动的应用程序,然后选择main class ,但是,如果您不知道ComponetScan的工作方式,那么如果不小心,将最终加载两个应用程序。

Another way is to use Profiles , you can set the profile when you start your spring app, and then have your web profile that will start Tomcat, and a command line profile that will not . 另一种方法是使用Profiles ,您可以在启动spring应用程序时设置配置文件,然后让您的Web配置文件启动Tomcat,而没有一个命令行配置文件启动Tomcat。

In the project I'm working on we have choosen to have the data-layer as a completly separate module (same gradle project), which has it's own Spring Context. 在我正在研究的项目中,我们选择将数据层作为一个完全独立的模块(同一gradle项目),它具有自己的Spring Context。 The data-layer spring context is then used as the parent context for other applications, as a reusable component. 然后,将数据层spring上下文用作其他应用程序的父上下文,作为可重用组件。 It is a somewhat cleaner separations of concerns, were the shared code is clearly marked, instead of having multiple applications inside the same code mudule. 如果清楚地标记了共享代码,而不是在同一个代码内包含多个应用程序,那么这是一个比较清晰的关注点分离。

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

相关问题 如何使用不可编译的类运行Spring Boot应用程序 - How to run Spring Boot application with not compileable classes 如何将 Firebase 与 Spring Boot REST 应用程序一起使用? - How to use Firebase with Spring boot REST Application? Spring 启动 - 如何在 rest 应用程序中验证 Multipartfile - Spring boot - How to validate Multipartfile in rest application Spring Boot 测试类可以重用应用程序上下文以加快测试运行速度吗? - Can Spring Boot test classes reuse application context for faster test run? 如何在 maven spring 启动应用程序中查看 lombok 生成的类 - How to see the classes generated by lombok in maven spring boot application 如何在 Spring 引导应用程序启动时自动验证 @Configuration 类? - How are @Configuration classes validated automatically on Start Up of Spring Boot application? 如何使用Spring Boot应用程序从Rest API返回HTML - How to return html from Rest API with spring boot application 如何将现有的 SOAP Web 服务调用到 REST API Z38008DD81C2F4D79185ECF6E0CE8AF1 启动应用程序 - How to invoke existing SOAP webservice into REST API Spring boot application 如何在 Spring Boot 应用程序中消费或调用另一个 Rest API? - How to consume or call another Rest API in Spring Boot application? 如何在 spring 引导中重用自定义响应 - How to reuse customized response in spring boot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM