Last Updated on 31 May 2022   |   Print Email
In this post, I’d like to share some code examples that demonstrate how to run code or logics right after a Spring Boot application’s startup. For example, loading some sample data into the database after application has started. Here, I mean the code runs ‘right after application startup’ as it gets executed after the Spring context has been instantiated - when all the beans have been created in the application context.There are several ways to do that with Spring Boot yet I share 2 primary ways: using CommandLineRunner and using application event listener.
1. Run code upon startup using CommandLineRunner
In a configuration class, declare a bean of type CommandLineRunnerlike this:
@Configuration
public class AppConfig {
@Bean
CommandLineRunner initDatabase() {
return args -> {
// insert some sample records upon startup...
};
}
}
This code inserts 3 rows of employee information into the database upon startup, with an EmployeeRepository bean injected by Spring automatically.Alternatively, you can create a @Component class that implements CommandLineRunner and override the run() method as shown in the following code snippet:
So as you have seen, to run your logics upon your Spring Boot application’s startup, you can either code a separate class that implements CommandLineRunner, or code bean of that type in a configuration class.
2. Run code upon startup using application event listener
Another way that can be used to run your code right after the Spring application context has been instantiated is implementing ApplicationListener interface in a component class, as shown in the below code example:
Note that the ContextRefreshEvent is raised when an application context is initialized or refreshed, which means the onApplicationEvent() method can be executed more than one time. So you may need to put a kind of state in your component to make sure the initialization code is executed only once.Alternatively, you can use the @EventListener annotation as shown in the following example:
So far you have learned some ways which you can use to run your own logics or custom code on startup of a Spring-based application. I hope you find this article helpful. Thanks for reading.
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.
Thanks for the above article. Am having one issue when i tried to implement the above CommandLineRunner interface ,application is able to run successfully in eclipse but when i tried to create JAR file it is not building in target folder where as other source files are getting created.
Comments
Thanks for the above article.
Am having one issue when i tried to implement the above CommandLineRunner interface ,application is able to run successfully in eclipse but when i tried to create JAR file it is not building in target folder where as other source files are getting created.
could you assist.