Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Spring boot: Ordering the execution of Application Runner (or) Command line runner

In my previous post, I explained about ApplicationRunner and CommandLineRunner interfaces. You can add multiple CommandLineRunner or ApplicationRunner interfaces to the spring boot application.

How to execute the runners in specific order?
By implementing org.springframework.core.Ordered interface or using the org.springframework.core.annotation.Order annotation, you can order the execution of runners.

Using @Order annotation
ApplicationRunner1.java
package com.sample.myApp.oneTimeExecutors;

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component
@Order(1)
public class ApplicationRunner1 implements ApplicationRunner {

@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println("Application Runner 1");
}

}

ApplicationRunner2.java

package com.sample.myApp.oneTimeExecutors;

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component
@Order(4)
public class ApplicationRunner2 implements ApplicationRunner {

@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println("Application Runner 2");
}

}

CommandLineRunner1.java

package com.sample.myApp.oneTimeExecutors;

import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component
@Order(2)
public class CommandLineRunner1 implements CommandLineRunner {

@Override
public void run(String... args) throws Exception {
System.out.println("Command line Runner 1");
}

}

CommandLineRunner2.java

package com.sample.myApp.oneTimeExecutors;

import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component
@Order(3)
public class CommandLineRunner2 implements CommandLineRunner {

@Override
public void run(String... args) throws Exception {
System.out.println("Command line Runner 2");
}

}

MyBean.java

package com.sample.myApp.model;

import org.springframework.stereotype.Component;

@Component
public class MyBean {
public MyBean() {
System.out.println("************************");
System.out.println("Initializing the bean");
System.out.println("************************");
}
}

Application.java

package com.sample.myApp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

import com.sample.myApp.model.MyBean;

@SpringBootApplication
public class Application {

public static void main(String args[]) {
ConfigurableApplicationContext applicationContext = SpringApplication.run(Application.class, args);

System.out.println("Initializaing bean first time");
applicationContext.getBean(MyBean.class);

System.out.println("Initializaing bean second time");
applicationContext.getBean(MyBean.class);
applicationContext.close();
}

}


When you ran ‘Application.java’, you can able to see below messages in the console.
Implementing Ordered interface

ApplicationRunner1.java

package com.sample.myApp.oneTimeExecutors;

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;

@Component
public class ApplicationRunner1 implements ApplicationRunner, Ordered {

@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println("Application Runner 1");
}

@Override
public int getOrder() {
return 1;
}

}


You can update other classes ApplicationRunner2, CommandLineRunner1, CommandLineRunner2 like above.






Previous                                                 Next                                                 Home


This post first appeared on Java Tutorial : Blog To Learn Java Programming, please read the originial post: here

Share the post

Spring boot: Ordering the execution of Application Runner (or) Command line runner

×

Subscribe to Java Tutorial : Blog To Learn Java Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×