Running code after Spring Boot starts is a common requirement for many applications. Spring Boot provides several ways to accomplish this, each with their own use cases and benefits. In this blog post, we will discuss the most popular ways to run code after Spring Boot starts.



Using the CommandLineRunner Interface

The CommandLineRunner interface is a simple and easy way to run code after Spring Boot starts. It has a single method, run, that takes the command-line arguments as input. To use this interface, you need to create a class that implements it, and then use the @Bean annotation to register it as a bean in the application context.

@Bean
public class MyCommandLineRunner implements CommandLineRunner {
  public void run(String... args) {
    // Your code here
  }
}


Using the ApplicationRunner Interface

The ApplicationRunner interface is similar to CommandLineRunner, but it receives the ApplicationArguments object instead of command-line arguments. This object provides more information about the arguments such as non-option arguments, option arguments, and more.

@Bean
public class MyApplicationRunner implements ApplicationRunner {
  public void run(ApplicationArguments args) {
    // Your code here
  }
}


Using the ApplicationReadyEvent

The ApplicationReadyEvent is triggered after the application context has been fully initialized and the application is ready to service requests. To use this event, you can create a class that implements the ApplicationListener interface and overrides the onApplicationEvent method.

@Component
public class ApplicationStartup implements ApplicationListener<ApplicationReadyEvent> {
    @Override
    public void onApplicationEvent(final ApplicationReadyEvent event) {
        // Your code here
    }
}

Alternatively, you can use the @EventListener annotation to handle the ApplicationReadyEvent event.

import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;

public class StartupEventHandler {

    @EventListener
    public void handleApplicationReadyEvent(ApplicationReadyEvent event) {
        // Your code here
    }
}


Using the @PostConstruct Annotation

The @PostConstruct annotation is part of the Java EE specification and can be used to specify a method that should be called after the bean has been constructed and all its dependencies have been injected. This method can be used to perform any initialization tasks that the bean may require.

@Component
public class MyBean {
  @PostConstruct
  public void init() {
    // Your code here
  }
}


Using Spring Boot’s @EventListener annotation

The @EventListener annotation is similar to the ApplicationListener interface, but it does not require you to create a separate class for the event listener. Instead, you can simply annotate a method with @EventListener and it will be called when the specified event is triggered.

@Component
public class MyEventHandler {
  @EventListener
  public void handleApplicationReadyEvent(ApplicationReadyEvent event) {
    // Your code here
  }
}


Using Spring Boot’s @Order annotation

If you have multiple CommandLineRunner or ApplicationRunner beans in your application and you need to control the order in which they are executed, you can use the @Order annotation to specify the order in which the beans should be executed.

@Bean
@Order(1)
public MyCommandLineRunner1 implements CommandLineRunner {
  public void run(String... args) {
    // Your code here
  }
}

@Bean
@Order(2)
public MyCommandLineRunner2 implements CommandLineRunner {
  public void run(String... args) {
    // Your code here
  }
}

In addition to these methods, there are also other ways of running code after Spring Boot starts, such as using the @DependsOn annotation to control the order of bean initialization or using the ApplicationContextAware interface to access the application context and perform tasks after the context has been fully initialized. With these options, you have many ways to run code after Spring Boot starts and you can choose the one that best fits your needs.



Summary

In conclusion, Spring Boot provides multiple ways to run code after the application starts. The CommandLineRunner and ApplicationRunner interfaces are easy to use, while the ApplicationReadyEvent is more flexible and provides more information about the application status. Choose the one that best fits your needs and enjoy coding with Spring Boot!



Leave a Reply