Scheduling method execution in Spring Boot can be done via the @EnableScheduling annotation which is a Spring Context module annotation. This meta-annotation internally imports the SchedulingConfiguration. The ScheduledAnnotationBeanPostProcessor that will be created by the imported configuration will scan the declared Spring Beans for the presence of the @Scheduled annotations. For every annotated method without arguments, the appropriate executor thread pool will be created. It will manage the scheduled invocation of the annotated method.

Here's an example which will fire the execution of a method every 30 seconds after an initial delay of 1 seconds:

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

@SpringBootApplication
@EnableScheduling
public class MainApplication {

	@Scheduled(initialDelay = 1000, fixedRate = 30000)
	public void run() {
		System.out.println("Called scheduler!");
	}
	public static void main(String[] args) {
		SpringApplication.run(MainApplication.class, args);
	}
}

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.

Spring Boot has an awesome feature named @ConfigurationProperties that lets you automatically bind the properties defined in the application.properties file into a Java Bean class. Let's see how to do it.