Explain Codes LogoExplain Codes Logo

Running code after Spring Boot starts

java
spring-boot-startup
application-ready-event
lifecycle-management
Alex KataevbyAlex Kataev·Mar 5, 2025
TLDR

If you want to execute certain logic post-startup in Spring Boot, leverage the CommandLineRunner coupled with @Component. This operation is triggered right after the application context gets up and running.

@Component public class PostStartupRunner implements CommandLineRunner { @Override public void run(String... args) { System.out.println("Just like waking up to a hot coffee, I run after Spring Boot startup!"); } }

Make this piece part of your codebase, and rest assured Spring Boot will take care of its execution right after the application fires up.

Diving Deep into Spring Boot Startup Actions

Utilizing Application Ready Event

An interesting and straightforward way to execute custom logic post start is to listen for the ApplicationReadyEvent via the @EventListener annotation. This ensures that all the @Autowired services are ready to roll, and your application is fully set to welcome HTTP requests.

@Component public class ApplicationStartup { @EventListener(ApplicationReadyEvent.class) public void doSomethingAfterStartup() { System.out.println("I'm as ready as a ninja on a mission after startup"); } }

Setting Priorities: Ordering your Startup Runners

In case you end up with multiple startup runners and need them to follow an exact sequence, implement the Ordered interface or use the @Order annotation. Control the order just like a chef controls the sequence of ingredients!

@Component @Order(1) public class FirstRunner implements CommandLineRunner { //... like the first ☕ of the day, gets you going } @Component @Order(2) public class SecondRunner implements CommandLineRunner { //... like the second ☕, keeps the momentum }

Advanced Lifecycle Control with SmartLifecycle

For scenarios demanding more control over lifecycle management, the SmartLifecycle interface is your friend. It allows you to run custom code in the midst of application startup, offering a higher level of precision compared to CommandLineRunner.

@Component public class CustomLifecycle implements SmartLifecycle { private boolean isRunning = false; @Override public void start() { // Custom code to run during the start-up phase, like a 🚀 taking off! } // Here you have other essential SmartLifecycle methods... }

Advanced Spring Initialization Techniques

Reacting to Bean Lifecycle Events with @PostConstruct

Take advantage of @PostConstruct to whip up some initialization logic post dependency injection but prior to the bean's debut.

@Component public class MyComponent { @PostConstruct public void init() { // Initialization logic here, like adjusting your 👓 before you read } }

Get a hold of Application Context and Manage Beans

If your use case involves managing beans directly post application firing, try injecting the ConfigurableApplicationContext.

@Component public class BeanManager { private final ConfigurableApplicationContext context; @Autowired public BeanManager(ConfigurableApplicationContext context) { this.context = context; } @PostConstruct public void manageBeans() { MyBean myBean = context.getBean(MyBean.class); // Here you are, free to operate on the bean, like a surgeon with a scalpel! } }

Servicing Web Applications with SpringBootServletInitializer

For your web application needs, overwriting SpringBootServletInitializer allows running code just after the servlet context comes to life.

public class MyWebApplication extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(MyApplication.class); } @Override public void onStartup(ServletContext servletContext) throws ServletException { super.onStartup(servletContext); // Your custom logic here! e.g., like a latecomer sneaking into a meeting 😉 } }

Heed the Lifecycle Events

Although, ApplicationPreparedEvent is another event you could listen to, do proceed with caution. Unlike me, this event can be premature and jumps the gun before your @Autowired setup is done causing potential null pointer exceptions.