Spring Boot CommandLineRunner

CommandLineRunner is an interface which allows us to write code that would be executed when the SpringBoot Application is initialized and before the startup is finished.

Technology used

  • Java 8
  • Spring boot 1.5.9
  • Logback 1.1.1.1
  • Maven 3.5.0

Implementation

This project uses Maven to build the project.  It shows a very simple example of a Spring boot Application and two Classes implementing CommandLineRunner.

High level steps as described bellow with sample code

Define the pom.xml file
Write Spring boot app class
Write Spring boot CommandLineRunner class
Change the order with Order annotation

For simplicity the pom file is not provided here, but it can be found on the git repo here https://github.com/siddharthagit/spring-boot-references/blob/master/spingboot-resttemplate/pom.xml

SpringBoot Application Class
ResttemplateApplication.java
@SpringBootApplication
public class ResttemplateApplication {

  private static final Logger log = LoggerFactory.getLogger(ResttemplateApplication.class);

  public static void main(String args[]) {
    log.info("about to call SpringApplication.run()");
    SpringApplication.run(ResttemplateApplication.class, args);
    log.info("completeed executing SpringApplication.run()");
  }

}
Next we will provide two Classes which implements CommandLineRunner interface.

LoadAllEndpoint loads all the available endpoints by calling  github's REST API
LoadAllEndpoint.java

@Component
@Order(1)
public class LoadAllEndpoint implements CommandLineRunner {

  private static final Logger log = LoggerFactory.getLogger(ResttemplateApplication.class);

  @Override
  public void run(String... args) throws Exception {
    log.info("about to call LoadGithubRepo.run()");
    RestTemplate restTemplate = new RestTemplateBuilder().build();
    ResponseEntity apis =
        restTemplate.getForEntity("https://api.github.com", JsonNode.class);
    
    //log.info(apis.toString());
  }
}
LoadGithubRepo returns all the available in github for an account by calling  github's REST API
LoadGithubRepo.java

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

  private static final Logger log = LoggerFactory.getLogger(ResttemplateApplication.class);

  @Override
  public void run(String... args) throws Exception {
    log.info("about to call LoadGithubRepo.run()");
    RestTemplate restTemplate = new RestTemplateBuilder().build();
    ResponseEntity repos = restTemplate
        .getForEntity("https://api.github.com/users/siddharthagit/repos", JsonNode.class);
    //log.info(repos.toString());
  }
}
Run the application

mvn spring-boot:run
Console Output (some part)

AnnotationMBeanExporter:431 - Registering beans for JMX exposure on startup
ResttemplateApplication:21 - about to call LoadAllEndpoint.run()
ResttemplateApplication:21 - about to call LoadGithubRepo.run()
ResttemplateApplication:57 - Started ResttemplateApplication in 2.07 seconds
ResttemplateApplication:17 - completed executing ResttemplateApplication.run()

Summary

CommandLineRunner is useful when we need to execute code before the Applicaton startup completes
We can have multiple implementations of CommandLineRunner
We can arrange the order of execution between different implementations using Order annotation

Download and Run

git clone https://github.com/siddharthagit/spring-boot-references.git
cd spingboot-resttemplate
mvn spring-boot:run

Comments

  1. JTG Casino in Biloxi, Mississippi - Use code NJ130 for $10 free + $1K
    JTG 원주 출장안마 Casino. JTG Casino 원주 출장샵 in 동두천 출장안마 Biloxi MS. 창원 출장안마 Get the 경산 출장샵 best value out of your stay. Click to see room rates for upcoming 2021 dates.

    ReplyDelete

Post a Comment

Popular posts from this blog

Difference between volatile and synchronized

Converting Java Map to String

Masking Credit Card number in Java