In order to execute method or classes when a Spring Boot application is started you can implement the CommandLineRunner interface, which basically provides just one method: public void run(String... args) that will get called by Spring Boot only once after the application has started.

Example:

package com.example;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.boot.CommandLineRunner;
public class StartupRunner implements CommandLineRunner {
	protected final Log logger = LogFactory.getLog(getClass());
	@Override
	public void run(String... args) throws Exception {
		logger.info("Start up Runner executed");
	}
}

After we have defined the class, let's proceed by defining it as @Bean in the main Application class:

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class MainApplication {
	@Bean
	public StartupRunner schedulerRunner() {
		return new StartupRunner();
	}
	public static void main(String[] args) {
		SpringApplication.run(MainApplication.class, args);
	}
}

Command line runners are the recommended way to execute Java code to be run just one, when the application starts up. The CommandLineRunner interface is used by Spring Boot to scan all of its implementations and invoke each instance's run method with the startup arguments.

Using Lambda Expression for CommandLineRunner

The following example shows a method that’s annotated with the @Bean annotation returning a CommandLineRunner implementation. This example uses the Java 8 syntax (lambda) to do the return. You can add as many methods that return a CommandLineRunner as you want.

package com.example;

import java.io.IOException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class MainApplication {
	private static final Logger log = LoggerFactory.getLogger
			(MainApplication.class);
	public static void main(String[] args) throws IOException {
		SpringApplication.run(MainApplication.class, args);
	}
	@Bean
	String info(){
		return "Hello World!";
	}
	@Autowired
	String info;
	@Bean
	CommandLineRunner myMethod(){
		return args -> {
			log.info("CommandLineRunner Implementation");
			log.info("Calling Info bean: " + info);
			for(String arg:args)
				log.info(arg);
		};
	}
}

Choosing the start up Order

We can also use an @Order annotation or implement an Ordered

interface so as to define the exact order in which we want Spring Boot to execute them. For

example, Spring Batch relies on the runners in order to trigger the execution of the jobs.