Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Web Application: doing something on startup (initialization)

I want to fill some tables of my DB from a text file on startup, I want my initialization method to be called only when my application do start.

I am using Spring (+MVC) and Hibernate with MySQL.

how can I do?

like image 563
Fabio B. Avatar asked Mar 13 '12 08:03

Fabio B.


1 Answers

You can create an application listener, it's designed specifically for such needs. In this case it will be executed every time context is started (or refreshed).

@Component
public class DatabaseFillerOnStartup implements ApplicationListener<ContextRefreshedEvent> {
    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        ...
    }
}
like image 120
sinuhepop Avatar answered Oct 11 '22 22:10

sinuhepop