Решение:
Я бы установил зависимость от FlywayMigrationInitializer
в конструкторе. Когда инициализатор создан и настроен, миграции выполняются.
Или вы можете зависеть от flywayInitializer
bean (@DependsOn("flywayInitializer")
). Компонент с именем flywayInitializer
класса FlywayMigrationInitializer
создан в FlywayAutoConfiguration.java
.
FlywayMigrationInitializer
реализует InitializingBean
и вызывает метод migrate
в методе afterPropertiesSet
.
Пример:
@Component
// @DependsOn("flywayInitializer")
@Slf4j
public class TestPostConstruct {
public TestPostConstruct(FlywayMigrationInitializer flywayForceInitialization) {
}
@PostConstruct
public void testPostConstruct() {
log.info("----> in testPostConstruct");
}
}
Журнал Spring Boot:
INFO 4760 --- [main] o.f.core.internal.command.DbMigrate : Successfully applied 1 migration to schema "PUBLIC" (execution time 00:00.130s)
INFO 4760 --- [main] c.example.flywayinit.TestPostConstruct : ----> in testPostConstruct