refeniz
refeniz

Reputation: 595

Flyway Spring Boot Autowired Beans with JPA Dependency

I am using Flyway 5.0.5 and I am unable to create a java (SpringJdbcMigration) with autowired properties... They end up null.

The closest thing I can find is this question: Spring beans are not injected in flyway java based migration

The answer mentions it being fixed in Flyway 5 but the links are dead.

What am I missing?

Upvotes: 13

Views: 14423

Answers (5)

refeniz
refeniz

Reputation: 595

I struggled with this for a long time due to my JPA dependency. I am going to edit the title of my question slightly to reflect this...

@Autowired beans are instantiated from the ApplicationContext. We can create a different bean that is ApplicationContextAware and use that to "manually wire" our beans for use in migrations.

A quite clean approach can be found here. Unfortunately, this throws an uncaught exception (specifically, ApplicationContext is null) when using JPA. Luckily, we can solve this by using the @DependsOn annotation and force flyway to run after the ApplicationContext has been set.

First we'll need the SpringUtility from avehlies/spring-beans-flyway2 above.

package com.mypackage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class SpringUtility implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    public void setApplicationContext(final ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    /*
        Get a class bean from the application context
     */
    public static <T> T getBean(final Class clazz) {
        return (T) applicationContext.getBean(clazz);
    }

    /*
        Return the application context if necessary for anything else
     */
    public static ApplicationContext getContext() {
        return applicationContext;
    }

}

Then, configure a flywayInitializer with a @DependsOn for springUtility. I extended the FlywayAutoConfiguration here hoping to keep the autoconfiguration functionality. This mostly seems to have worked for me, except that turning off Flyway in my gradle.build file no longer works, so I had to add the @Profile("!integration") to prevent it from running during my tests. Other than that the autoconfiguration seems to work for me but admittedly I've only run one migration. Hopefully someone will correct me if I am wrong.

package com.mypackage;

import org.flywaydb.core.Flyway;
import org.springframework.boot.autoconfigure.flyway.FlywayMigrationInitializer;
import org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration.FlywayConfiguration;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.DependsOn;

import com.mypackage.SpringUtility;

@Configuration
@Profile("!integration")
class MyFlywayConfiguration extends FlywayConfiguration {
    @Primary
    @Bean(name = "flywayInitializer")
    @DependsOn("springUtility")
    public FlywayMigrationInitializer flywayInitializer(Flyway flyway){
        return super.flywayInitializer(flyway);
        //return new FlywayMigrationInitializer(flyway, null);
    }
}

And just to complete the example, here is a migration:

package db.migration;

import org.flywaydb.core.api.migration.spring.BaseSpringJdbcMigration;
import org.springframework.jdbc.core.JdbcTemplate;

import org.springframework.stereotype.Component;

import com.mypackage.repository.AccountRepository;
import com.mypackage.domain.Account;

import com.mypackage.SpringUtility;

import java.util.List;

public class V2__account_name_ucase_firstname extends BaseSpringJdbcMigration {

    private AccountRepository accountRepository = SpringUtility.getBean(AccountRepository.class);

    public void migrate(JdbcTemplate jdbcTemplate) throws Exception {

        List<Account> accounts = accountRepository.findAll();

        for (Account account : accounts) {

            String firstName = account.getFirstName();
            account.setFirstName(firstName.substring(0, 1).toUpperCase() + firstName.substring(1));
            account = accountRepository.save(account);

        }
    }
}

Thanks to avehlies on github, Andy Wilkinson on Stack Overflow and OldIMP on GitHub for helping me along the way.

In case you are using more recent versions of Flyway, then extend BaseJavaMigration instead of BaseSpringJdbcMigration as the latter is deprecated. Also, take a look at the below two comments by the user Wim Deblauwe.

Upvotes: 20

tangella lokesh
tangella lokesh

Reputation: 1

Current flyway 6.5.5 version is released and back from 6.0.0 I believe support for spring beans is provided. You can directly autowire spring beans into your Java based migrations (using @autowired), But the hunch is your Migration class also should be managed by Spring to resolve dependency. There is a cool and simple way for it, by overriding default behavior of Flyway, check out https://reflectoring.io/database-migration-spring-boot-flyway/ the article clearly answers your question with code snippets.

Upvotes: -1

Rohde Fischer
Rohde Fischer

Reputation: 1482

Seems the updated answer provided by @mararn1618 is under documented on the official documentation, so I will provide a working setup here. Thanks to @mararn1618 for guiding in that direction.

Disclaimer, it's written in Kotlin :)

First you need a configuration for loading the migration classes, in Spring Boot (and perhaps Spring) you need either an implementation of FlywayConfigurationCustomizer or a setup of FlywayAutoConfiguration.FlywayConfiguration. Only the first is tested, but both should work

Configuration a, tested

import org.flywaydb.core.api.configuration.FluentConfiguration
import org.flywaydb.core.api.migration.JavaMigration
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.autoconfigure.flyway.FlywayConfigurationCustomizer
import org.springframework.context.ApplicationContext
import org.springframework.stereotype.Component

@Component
class MyFlywayConfiguration @Autowired constructor(
        val applicationContext: ApplicationContext
) : FlywayConfigurationCustomizer {
    override fun customize(configuration: FluentConfiguration?) {
        val migrationBeans = applicationContext.getBeansOfType(JavaMigration::class.java)
        val migrationBeansAsArray = migrationBeans.values.toTypedArray()
        configuration?.javaMigrations(*migrationBeansAsArray)
    }
}

Configuration option B, untested, but should also work

import org.flywaydb.core.api.migration.JavaMigration
import org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration
import org.springframework.boot.autoconfigure.flyway.FlywayConfigurationCustomizer
import org.springframework.context.ApplicationContext
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration

@Configuration
class MyFlywayConfiguration : FlywayAutoConfiguration.FlywayConfiguration() {
    @Bean
    fun flywayConfigurationCustomizer(applicationContext: ApplicationContext): FlywayConfigurationCustomizer {
        return FlywayConfigurationCustomizer { flyway ->
            val p = applicationContext.getBeansOfType(JavaMigration::class.java)
            val v = p.values.toTypedArray()

            flyway.javaMigrations(*v)
        }
    }
}

And with that you can just write your migrations as almost any other Spring bean:

import org.flywaydb.core.api.migration.BaseJavaMigration
import org.flywaydb.core.api.migration.Context
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component

@Component
class V7_1__MyMigration @Autowired constructor(
) : BaseJavaMigration() {
    override fun migrate(context: Context?) {
        TODO("go crazy, mate, now you can import beans, but be aware of circular dependencies")
    }
}

Side notes:

  • Be careful of circular dependencies, your migrations can most likely not depend on repositories (also makes sense, you are preparing them, after all)
  • Make sure your migrations are located where Spring scans for classes. So if you want to place them in the namespace db/migrations, you need to ensure that Spring scans that location
  • I haven't tested, but it's likely one should be cautious with mixing the path for these migrations and the locations where Flyway scans for migrations

Upvotes: 3

Benedikt Gansinger
Benedikt Gansinger

Reputation: 417

If you are using deltaspike you can use BeanProvider to get a reference to your DAO.

Change your DAO code:

public static UserDao getInstance() {
    return BeanProvider.getContextualReference(UserDao.class, false, new DaoLiteral());
}

Then in your migration method:

UserDao userdao = UserDao.getInstance();

And there you've got your reference.

(referenced from: Flyway Migration with java)

Upvotes: -1

Andy Wilkinson
Andy Wilkinson

Reputation: 116091

The functionality hasn't made it into Flyway yet. It's being tracked by this issue. At the time of writing that issue is open and assigned to the 5.1.0 milestone.

Upvotes: 4

Related Questions