How to Execute Code on Spring Application Start-Up
Have you ever encountered a situation where you’ve to perform some tasks immediately after the Spring/SpringBoot application starts?
Join the DZone community and get the full member experience.
Join For FreeIntroduction
Have you ever encountered a situation where you’ve to perform some tasks immediately after the Spring/SpringBoot application starts? i.e. Initialize some data into the database, initialize application-level constants, make an API call, etc.
There are several ways to achieve it. Here I’m gonna discuss:
- Application events
- ApplicationRunner
Technologies Used
- Java 11
- Spring Boot 2.2.4
- Gradle 6.0.1
Application Events
The Spring framework triggers various events. For our use case, we’ll be more interested in ContextStartedEvent
and ContextRefreshedEvent
. ContextStartedEvent event triggered at the time of context gets started. ContextRefreshedEvent event triggered at the time of context gets started or refreshed.
public class EventHandler {
ContextStartedEvent.class) (
public void handleContextStartEvent(ContextStartedEvent e) {
// Write your code here
}
ContextRefreshedEvent.class) (
public void handleContextRefreshEvent(ContextRefreshedEvent e) {
// Write your code here
}
// Or you can handle both the events in 1 method
ContextStartedEvent.class, ContextRefreshedEvent.class}) ({
public void handleBoth(ApplicationContextEvent e) {
if (e instanceof ContextStartedEvent) {
} else {
}
}
}
ApplicationRunner
SpringBoot provides an interface called ApplicationRunner
, any bean implementing this interface should run when that contained in the SpringApplication
.
xxxxxxxxxx
public class DBInitializer implements ApplicationRunner {
private final UserRepository userRepository;
private DBInitializer(UserRepository userRepository) {
this.userRepository = userRepository;
}
public void run(ApplicationArguments args) throws Exception {
// Initialize user here
}
}
or the above can be used
public class Config {
public ApplicationRunner initializeUser(UserRepository userRepository) {
return args -> {
// Initialize user here
};
}
}
ApplicationRunner provides ApplicationArguments
in the run method which is used to get command line arguments by invoking getSourceArgs()
. You can also get the parsed arguments using this class. i.e.
Let’s say you’ve passed command-line arguments like --source /usr/local --print-only --target /tmp/local
So the method call to
getOptionNames()
inApplicationArguments
will return set of arguments - [‘source’, ‘print-only’, ‘target’]containsOption(String name)
checks if the argument contains in the given inputgetOptionValues(name)
returns a list of option values.getOptionValues('source')
will return list - [‘/usr/local’]
Published at DZone with permission of Jitendra Bisht. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
How To Integrate Microsoft Team With Cypress Cloud
-
Web Development Checklist
-
Top 10 Pillars of Zero Trust Networks
-
Merge GraphQL Schemas Using Apollo Server and Koa
Comments