javaspring-bootconfigurationflywaymigrate

Spring Boot Autowire Service inside custom Flywayconfiguration


I use Flyway 8.5 and Springboot 2.7. I am trying to call a function from a service after the migration of flyway has been completed (i.e. at an After_Migrate event) which adjusts the database again.

@Configuration
public class FlywayConfiguration implements FlywayConfigurationCustomizer {

    @Override
    public void customize(FluentConfiguration configuration) {
        configuration.callbacks(new FlywayCallback());
    }

}
public class FlywayCallback extends BaseCallback {

    @Autowired
    @Lazy
    private ProfileService profileService;
    
    @Override
    public void handle(Event event, Context context) {
        if (event == Event.AFTER_MIGRATE) {
            profileService.setMissingThumbnailsForProfiles();
        }
    }

}

However it doesn't quite work as profileService is always null.

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flywayInitializer' defined in class path resource [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]: Invocation of init method failed; nested exception is org.flywaydb.core.api.FlywayException: Error while executing afterMigrate callback: null
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1804) ~[spring-beans-5.3.27.jar:5.3.27]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:620) ~[spring-beans-5.3.27.jar:5.3.27]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.27.jar:5.3.27]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.27.jar:5.3.27]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.27.jar:5.3.27]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.27.jar:5.3.27]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.27.jar:5.3.27]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322) ~[spring-beans-5.3.27.jar:5.3.27]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.27.jar:5.3.27]
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1156) ~[spring-context-5.3.27.jar:5.3.27]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:910) ~[spring-context-5.3.27.jar:5.3.27]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.27.jar:5.3.27]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:147) ~[spring-boot-2.7.11.jar:2.7.11]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:731) ~[spring-boot-2.7.11.jar:2.7.11]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408) ~[spring-boot-2.7.11.jar:2.7.11]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) ~[spring-boot-2.7.11.jar:2.7.11]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1303) ~[spring-boot-2.7.11.jar:2.7.11]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1292) ~[spring-boot-2.7.11.jar:2.7.11]
    at de.openknowledge.mipro2.Mipro2Application.main(Mipro2Application.java:11) ~[classes/:na]
Caused by: org.flywaydb.core.api.FlywayException: Error while executing afterMigrate callback: null
    at org.flywaydb.core.internal.callback.DefaultCallbackExecutor.handleEvent(DefaultCallbackExecutor.java:136) ~[flyway-core-8.5.13.jar:na]
    at org.flywaydb.core.internal.callback.DefaultCallbackExecutor.execute(DefaultCallbackExecutor.java:129) ~[flyway-core-8.5.13.jar:na]
    at org.flywaydb.core.internal.callback.DefaultCallbackExecutor.lambda$execute$0(DefaultCallbackExecutor.java:116) ~[flyway-core-8.5.13.jar:na]
    at org.flywaydb.core.internal.jdbc.TransactionalExecutionTemplate.execute(TransactionalExecutionTemplate.java:55) ~[flyway-core-8.5.13.jar:na]
    at org.flywaydb.core.internal.callback.DefaultCallbackExecutor.execute(DefaultCallbackExecutor.java:114) ~[flyway-core-8.5.13.jar:na]
    at org.flywaydb.core.internal.callback.DefaultCallbackExecutor.onMigrateOrUndoEvent(DefaultCallbackExecutor.java:70) ~[flyway-core-8.5.13.jar:na]
    at org.flywaydb.core.internal.command.DbMigrate.migrate(DbMigrate.java:114) ~[flyway-core-8.5.13.jar:na]
    at org.flywaydb.core.Flyway$1.execute(Flyway.java:172) ~[flyway-core-8.5.13.jar:na]
    at org.flywaydb.core.Flyway$1.execute(Flyway.java:124) ~[flyway-core-8.5.13.jar:na]
    at org.flywaydb.core.FlywayExecutor.execute(FlywayExecutor.java:205) ~[flyway-core-8.5.13.jar:na]
    at org.flywaydb.core.Flyway.migrate(Flyway.java:124) ~[flyway-core-8.5.13.jar:na]
    at org.springframework.boot.autoconfigure.flyway.FlywayMigrationInitializer.afterPropertiesSet(FlywayMigrationInitializer.java:66) ~[spring-boot-autoconfigure-2.7.11.jar:2.7.11]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1863) ~[spring-beans-5.3.27.jar:5.3.27]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1800) ~[spring-beans-5.3.27.jar:5.3.27]
    ... 18 common frames omitted
Caused by: java.lang.NullPointerException: null
    at de.openknowledge.mipro2.infrastructure.FlywayCallback.handle(FlywayCallback.java:20) ~[classes/:na]
    at org.flywaydb.core.internal.callback.DefaultCallbackExecutor.handleEvent(DefaultCallbackExecutor.java:134) ~[flyway-core-8.5.13.jar:na]
    ... 31 common frames omitted


Process finished with exit code 1

I also tried to This approach which results in a cyclic bean exception.


Solution

  • @Autowired is being ignored as you're creating the instance of FlywayCallback and Spring knows nothing about it:

    configuration.callbacks(new FlywayCallback());
    

    This means that it isn't a Spring-managed bean. To make it a Spring-managed bean you could, assuming its package is covered by component scanning, annotate it with @Component. Alternatively, you could return an instance of it from a @Bean method in a @Configuration class.