The blog is originally published on my blog jsblogs.github.io
Introduction
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 database, initialize application level constants, make an API call, etc.
There are several ways to achieve it. Here I'm gonna discuss about:
- 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.
@Component
public class EventHandler {
@EventListener(ContextStartedEvent.class)
public void handleContextStartEvent(ContextStartedEvent e) {
// Write your code here
}
@EventListener(ContextRefreshedEvent.class)
public void handleContextRefreshEvent(ContextRefreshedEvent e) {
// Write your code here
}
// Or you can handle both the events in 1 method
@EventListener({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
.
@Component
public class DBInitializer implements ApplicationRunner {
private final UserRepository userRepository;
private DBInitializer(UserRepository userRepository) {
this.userRepository = userRepository;
}
@Override
public void run(ApplicationArguments args) throws Exception {
// Initialize user here
}
}
or the above can be used
@Configuration
public class Config {
@Bean
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 argument contains -
getOptionValues(name)
returns list of option values.getOptionValues('source')
will return list - ['/usr/local']
Top comments (0)