I would like to use in my Spring Boot application flywaydb to update / initialize database from SQL alter files on initialization. Java code, that process Flywaydb, should be executed after Spring datasource is created (Flywaydb needs javax.sql.DataSource) BUT before application beans are initialized. I am aware of @DependsOn annotation bud I don't want to avoid to set this on all application beans.
Is there some way how to initialize specific bean in specified order?
Just don't do anything special. Spring Boot has support for Flyway out-of-the-box. There are only 3 steps to take
src/main/resources/db/migrationYou don't need to add annotations, create beans or use callbacks. The Spring Team already thought of that.
If you want a bean callback to happen exactly once when the application is ready to start, all beans are ready and will not get called again when the context is refreshed then a good Spring Boot recipe is to use the ApplicationReadyEvent like this:
@EventListener(ApplicationReadyEvent.class)
public void onApplicationEvent(ApplicationReadyEvent event) {
// do tasks that should happen once on startup
}