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 CommandLineRunner like this:

@Configuration
public class AppConfig {	
	
	@Bean
	CommandLineRunner initDatabase() {
		
		return args -> {
			
			// insert some sample records upon startup...
			
		};
	}
}

Below is a more concrete example:

package net.codejava;

import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AppConfig {
	
	@Bean
	CommandLineRunner initDatabase(EmployeeRepository employeeRepository) {
		
		return args -> {
			
			employeeRepository.save(new Employee(...));	// insert employee 1
			employeeRepository.save(new Employee(...));	// insert employee 2
			employeeRepository.save(new Employee(...));	// insert employee 3
		
		};
	}
}

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:

package net.codejava;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class LoadDatabase implements CommandLineRunner {
	
	private final EmployeeRepository employeeRepository;
	
	@Autowired
	public LoadDatabase(EmployeeRepository employeeRepository) {
		this.employeeRepository = employeeRepository;
	}

	@Override
	public void run(String... args) throws Exception {
		
		employeeRepository.save(new Employee(...));	// insert employee 1
		employeeRepository.save(new Employee(...));	// insert employee 2
		employeeRepository.save(new Employee(...));	// insert employee 3
	}

}

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:

package net.codejava;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;


@Component
public class AppStartupListener implements ApplicationListener<ContextRefreshedEvent> {

	private final EmployeeRepository employeeRepository;
	
	@Autowired
	public AppStartupListener(EmployeeRepository employeeRepository) {
		this.orderRepository = orderRepository;
	}	
	
	@Override
	public void onApplicationEvent(ContextRefreshedEvent event) {
		
		employeeRepository.save(new Employee(...));	// insert employee 1
		employeeRepository.save(new Employee(...));	// insert employee 2
		employeeRepository.save(new Employee(...));	// insert employee 3

	}
}

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:

package net.codejava;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;


@Component
public class StartupEventListener {
	
	private final EmployeeRepository employeeRepository;
	
	@Autowired
	public StartupEventListener(EmployeeRepository employeeRepository) {
		this.employeeRepository = employeeRepository;
	}		
	
	@EventListener
	public void onApplicationEvent(ContextRefreshedEvent event) {
		
		employeeRepository.save(new Employee(...));	// insert employee 1
		employeeRepository.save(new Employee(...));	// insert employee 2
		employeeRepository.save(new Employee(...));	// insert employee 3
		
	}
}

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.

 

Tutorials:


About the Author:

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.



Add comment

   


Comments 

#1vikas2024-03-20 11:58
Hi,

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.
Quote