nitinkc
nitinkc

Reputation: 53

Let only the run method execute (from CommandLineRunner or ApplicationRunner), and then stop without the Spring boot Application startup

I have a requirement of running a small piece of code (like a batch job) on demand. For example i want to execute my Spring boot code as java -jar build/libs/my-springboot-application-1.0.0.jar.

I have achieved this by writing my piece of code by overriding the run method of CommandLineRunner or ApplicationRunner. It works fine. The only problem is, once the method executes, the application starts running on the embedded server, as per the default behavior, but this is not required in my case. There is no requirement of having the spring boot application run.

All i need is to execute my logic from the class that implements commandLineRunner/Application runner and exit. Is it possible with Spring boot using java 11? In short, i want to alter the default behavior and quit right after application context is loaded, the run method is executed, without spring boot application startup.

Upvotes: 0

Views: 724

Answers (1)

MBill
MBill

Reputation: 41

Try using this:

ConfigurableApplicationContext context = new SpringApplicationBuilder(MainApp.class)
            .web(WebApplicationType.NONE).run();

This will create context without spring web support

Upvotes: 1

Related Questions