在 Sprint Boot Web 应用程序中运行脚本 [英] Running Scripts in a Sprint Boot Web Application

查看:27
本文介绍了在 Sprint Boot Web 应用程序中运行脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个打包为 WAR 文件的 Spring Boot Web 应用程序,我希望能够按需运行某些任务(脚本或命令).例如:

I have a Spring Boot web application packaged as a WAR file and I wish to have the ability to run some tasks (scripts or commands) on-demand. For example:

  • 初始化数据库架构
  • 编程方式
  • 为数据库播种
  • 新版本应用程序需要时进行数据迁移(移动文件、数据库操作等)
  • 验证存储文件和数据库内容之间的数据完整性

如果这些命令"能够访问与 Web 应用程序相同的 bean(服务、DAO)和模型,那么它们将更容易实现.我希望我必须在某处创建一个 Spring 应用程序上下文才能发生这种情况.我还希望将这些命令与应用程序的其余部分打包在一起,以便与应用程序一起进行测试并易于部署.

These "commands" would be much easier to implement if they had access to the same beans (services, DAOs) and models as the web app proper. I expect I have to create a Spring application context somewhere for this to happen. I would also prefer to package these commands with the rest of the application so they are tested alongside it and easy to deploy.

本质上,我想知道如何实现类似于 Laravel 的工匠命令(PHP).

Essentially, I want to know how to achieve something similar to Laravel's artisan commands (PHP).

我读过在 WAR 中运行类 此处这里.我正在寻找打包和运行命令的Spring 方式".我是否必须集成 Spring Batch 作业 为此?据我了解,Spring Batch 适用于可以拆分为多个批次的较大工作负载.我应该咬紧牙关将命令放在引用第一个的第二个单独的 maven 模块中吗?

I have read on running a class inside a WAR here and here. I'm searching for the "Spring way" of packaging and running commands. Do I have to integrate Spring Batch jobs for this? From what I understand, Spring Batch is meant for larger workloads which can be split in multiple batches. Should I bite the bullet and put the commands in a second separate maven module which references the first one?

我实现了一个 ApplicationRunner(感谢 Stefan!)查看它们是如何工作的,发现它们在 Spring 应用程序上下文刷新后运行.这意味着整个 Web 应用程序在运行程序执行之前已初始化并处于活动状态.这包括配置中定义的 MVC 端点映射器和计划任务.虽然这绝对有用,但我需要在所有未激活的情况下运行命令,因为它可能与命令发生冲突(尤其是命令运行期间的瞬态数据状态).

I implemented an ApplicationRunner (thanks Stefan!) to see how they work and found that they run after the Spring application context is refreshed. This means that the whole web application is initialized and active before the runner executes. This includes MVC endpoint mappers and scheduled tasks defined in configurations. While this can definitely be useful, I need to run commands when all that is not active because it can come in conflict with the commands (especially with transient data states during command runs).

我只需要创建另一个使用 CommandLineRunner 创建 Spring Boot 应用程序的可执行 jar.如果我使用 3 个模块,这可以引用 Web 应用程序中的所有 bean:

I simply needed to create another executable jar that creates a Spring Boot application using a CommandLineRunner. This can reference all the beans from the web application if I use 3 modules:

  1. spring服务"bean 模块
  2. Web 应用程序模块
  3. 命令行运行模块

推荐答案

可以使用 ApplicationRunner见 spring boot 应用程序运行程序.运行程序在应用启动后执行,您可以访问 spring 上下文.

You can use ApplicationRunner see spring boot application runner. The runners are executed after the app started and you have access to the spring context.

CommandLineRunner 类正是为此而使用的.另请参阅基于 Spring Boot 控制台的应用程序如何工作?

The CommandLineRunner class is used precisely for this. See also How does a Spring Boot console based application work?

这篇关于在 Sprint Boot Web 应用程序中运行脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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