Search code examples
spring-bootflyway

Autowiring not working inside flyway callback methods


I am trying to flyway for the migration demo. In which I am able to migrate code. I have extended BaseFlywayCallback and I want to print migration info before and after each migration. I have code in other service class for printing info. I am auto wiring it but it is not working there. I have tried some other ways but nothing worth it.

  @Autorired
  PrintInfoService service

How can I do that?


Solution

  • The integration with flyway internally in spring boot is done by means of using FlywayAutoConfiguration

    It has to create a Flyway bean on which it will register all callbacks found in the classpath.

    I can't test this, but you I believe you have to:

    1. Make sure that your callback is a spring bean
    2. Make sure that spring boot scans your bean and create an instance of it during the initialization
    3. Place a breakpoint in the FlywayAutoConfiguration's:
    public Flyway flyway(FlywayProperties properties, DataSourceProperties dataSourceProperties,
                    ResourceLoader resourceLoader, ObjectProvider<DataSource> dataSource,
                    @FlywayDataSource ObjectProvider<DataSource> flywayDataSource,
                    ObjectProvider<FlywayConfigurationCustomizer> fluentConfigurationCustomizers,
                    ObjectProvider<JavaMigration> javaMigrations, ObjectProvider<Callback> callbacks)
    
    1. ... And make sure that your callback is in the list of beans recognized by spring.