不会在基于Flyway Java的迁移中注入Spring Bean [英] Spring beans are not injected in flyway java based migration

查看:115
本文介绍了不会在基于Flyway Java的迁移中注入Spring Bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在flyway迁移Java代码中注入配置属性的组件,但它始终为null.

I'm trying to inject component of configuration properties in the flyway migration java code but it always null.

我在Flyway上使用弹簧靴.

I'm using spring boot with Flyway.

@Component
@ConfigurationProperties(prefix = "code")
public class CodesProp {

    private String codePath;
 }

然后在Flyway迁移代码中尝试按以下方式自动编写此组件:

Then inside Flyway migration code, trying to autowrire this component as following:

public class V1_4__Migrate_codes_metadata implements SpringJdbcMigration {

@Autowired
private CodesProp codesProp ;
public void migrate(JdbcTemplate jdbcTemplate) throws Exception {
    codesProp.getCodePath();  
}

在这里,codesProp始终为空.

Here, codesProp is always null.

有什么方法可以在弹道内注入弹簧豆,或者在弹道豆之前对其进行初始化?

Is there any way to inject spring beans inside flyway or make it initialized before flyway bean?

谢谢.

推荐答案

Flyway不支持将依赖项注入到SpringJdbcMigration实现中.它只是在类路径上查找实现SpringJdbcMigration的类,并使用默认构造函数创建一个新实例.这是在

Flyway doesn't support dependency injection into SpringJdbcMigration implementations. It simply looks for classes on the classpath that implement SpringJdbcMigration and creates a new instance using the default constructor. This is performed in SpringJdbcMigrationResolver. When the migration is executed, SpringJdbcMigrationExecutor creates a new JdbcTemplate and then calls your migration implementation's migrate method.

如果确实需要将依赖项注入基于Java的迁移中,我认为您必须实现自己的MigrationResolver,该MigrationResolver从应用程序上下文中检索特定类型的bean,并创建并返回ResolvedMigration每个实例.

If you really need dependencies to be injected into your Java-based migrations, I think you'll have to implement your own MigrationResolver that retrieves beans of a particular type from the application context and creates and returns a ResolvedMigration instance for each.

这篇关于不会在基于Flyway Java的迁移中注入Spring Bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆