Using ApplicationReadyEvent to run code at Spring Boot startup

In Spring Boot, ApplicationReadyEvent is an event triggered when the application is up and ready to receive requests. You can use this event to execute specific code or perform tasks right after the application has fully started.

Prerequisites

  • Basic understanding of Java and Spring Boot

Step 1: Create a Spring Boot Project

If you don’t have an existing Spring Boot project, create a new one using your preferred IDE or the Spring Initializr.

Step 2: Implement an ApplicationReadyEvent Listener

Create a component that listens for the ApplicationReadyEvent and executes code when the event occurs.

For example, add the following Class to your Spring Boot project:

package com.example.testrest;

import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

@Component public class ApplicationStartup implements ApplicationListener < ApplicationReadyEvent > {
  @Override public void onApplicationEvent(final ApplicationReadyEvent event) {
    System.out.println("Application started!");
    return;
  }
}

When you start the application, you will see from the Server Logs:

spring boot how to execute code on start up

Capturing multiple Events on Startup

If you want to capture multiple events, such as a failure in application startup, you can use @EventListener annotation which can be associated with a ContextRefreshedEvent, an ApplicationReadyEvent and an ApplicationFailedEvent:

@SpringBootApplication
@EnableScheduling
public class MyApplication {
  public static void main(String[] args) throws Exception {
    SpringApplication.run(MyApplication.class, args);
  }

  @EventListener(ContextRefreshedEvent.class)
  public void ContextRefreshedEventExecute() {
    System.out.println("Context Event Listener is getting executed");
  }

  @EventListener(ApplicationReadyEvent.class)
  public void EventListenerExecute() {
    System.out.println("Application Ready Event is successfully Started");
  }

  @EventListener(ApplicationFailedEvent.class)
  public void EventListenerExecuteFailed() {
    System.out.println("Application Event Listener is Failed");
  }
}

In terms of configuration, you don’t need any special library to trigger the ApplicationReadyEvent. Just include the web starter:

<?xml version="1.0" encoding="UTF-8"?><project>
   <dependency>
       	
      <groupId>org.springframework.boot</groupId>
       	
      <artifactId>spring-boot-starter-web</artifactId>
       
   </dependency>
    
</project>

Other options to trigger code at startup

The ApplicationReadyEvent is not the only available option to run code when the Spring Boot application starts up. As a matter of fact, you can also use the run() method of CommandLineRunner to execute synchronous code when the application is fully operational. Check this article to learn more: How to execute a Java class at Spring Boot startup

Conclusion

Utilizing the ApplicationReadyEvent in Spring Boot allows you to execute specific tasks or code precisely when your application is fully started and ready to handle requests. This feature is beneficial for performing various startup-related actions. It is not the only option available.

Feel free to modify the executeOnStartup() method to suit your application’s specific needs during startup.

Found the article helpful? if so please follow us on Socials
Twitter Icon       Facebook Icon       LinkedIn Icon       Mastodon Icon