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:
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.
Essentially, I want to know how to achieve something similar to Laravel's artisan commands (PHP).
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?
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).
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:
Spring Boot Starter Web provides all the dependencies and the auto configuration needed to develop web applications. We should use the first dependency. We want to use JSP as the view. Default embedded servlet container for Spring Boot Starter Web is tomcat.
You can run a Spring Boot application from your IDE as a simple Java application. However, you first need to import your project. Import steps vary depending on your IDE and build system. Most IDEs can import Maven projects directly.
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.
The CommandLineRunner
class is used precisely for this. See also How does a Spring Boot console based application work?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With