Friday, 27 July 2018

Spring boot: ApplicationRunner: Run the code only once

If you want to run some specific code only once, after the SpringApplication has started, you can implement the ApplicationRunner or CommandLineRunner interfaces.

Both ApplicationRunner or CommandLineRunner interfaces has run method, which is called just before SpringApplication.run(…) completes.

In this post, I am going to explain about the usage of ApplicationRunner interface.

@FunctionalInterface
public interface ApplicationRunner {

         void run(ApplicationArguments args) throws Exception;

}

Find the below working application.

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("************************");
 }
}

SingleTimeExecutor.java
package com.sample.myApp.model;

import java.util.List;
import java.util.Set;

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

@Component
public class SingleTimeExecutor implements ApplicationRunner {

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

  System.out.println("Option Arguments are :");
  Set<String> optionNames = args.getOptionNames();
  for (String optionName : optionNames) {
   System.out.println(optionName + " : " + args.getOptionValues(optionName));
  }

  System.out.println("Non Option Arguments are : ");
  List<String> nonOptionArgs = args.getNonOptionArgs();
  for (String arg : nonOptionArgs) {
   System.out.println(arg);
  }

  System.out.println("Source args are:");
  String[] sourceArgs = args.getSourceArgs();
  for (String sourceArg : sourceArgs) {
   System.out.println(sourceArg);
  }

  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();
 }

}

Run 'Application.java', by passing command line arguments '--landscape=development --version=14.3 krishna'. you can able to see below messages in console.

************************
Initializing the bean
************************
************************
Executing using Application Runner
Option Arguments are :
version : [14.3]
landscape : [development]
Non Option Arguments are : 
krishna
Source args are:
--landscape=development
--version=14.3
krishna
************************
Initializaing bean first time
Initializaing bean second time




Previous                                                 Next                                                 Home

No comments:

Post a Comment