简体   繁体   English

如何在 Mongock 更新日志中注入没有接口的 bean

[英]How to inject beans without interface in Mongock changelogs

I am in the process of migrate a spring boot application from 2.2.2 to 2.2.3.我正在将 Spring Boot 应用程序从 2.2.2 迁移到 2.2.3。 I also upgrade mongock to 4.1.16 as the version 2.0.2 used so far is not compatible anymore.我还将 mongock 升级到 4.1.16,因为到目前为止使用的 2.0.2 版本不再兼容。

I have this changelog which works fine in 2.0.2 but not in 4.1.16 :我有这个更新日志,它在 2.0.2 中运行良好,但在 4.1.16 中不起作用:

    @ChangeSet(order = "001", id = "initDatabaseParametre", author = "xxxxx")
    public void initDatabaseParametre(ParametreManager parametreManager, ObjectMapper mapper) throws IOException 
        // someting
    }

With 4.1.16 I have this exception because ObjectMapper is not an interface (com.fasterxml.jackson.databind.ObjectMapper) :使用 4.1.16 我有这个例外,因为 ObjectMapper 不是一个接口 (com.fasterxml.jackson.databind.ObjectMapper) :

io.changock.migration.api.exception.ChangockException: Error in method[ChangelogInitDatabase.initDatabaseParametre] : Parameter of type [ObjectMapper] must be an interface
        at io.changock.runner.core.MigrationExecutor.processExceptionOnChangeSetExecution(MigrationExecutor.java:179)
        at io.changock.runner.core.MigrationExecutor.processSingleChangeSet(MigrationExecutor.java:97)
        at io.changock.runner.core.MigrationExecutor.lambda$processSingleChangeLog$2(MigrationExecutor.java:89)
        at io.changock.runner.core.MigrationExecutor.executeInTransactionIfStrategyOrUsualIfNot(MigrationExecutor.java:75)
        at io.changock.runner.core.MigrationExecutor.processSingleChangeLog(MigrationExecutor.java:89)
        at io.changock.runner.core.MigrationExecutor.lambda$processAllChangeLogs$1(MigrationExecutor.java:83)
        at io.changock.runner.core.MigrationExecutor.executeInTransactionIfStrategyOrUsualIfNot(MigrationExecutor.java:75)
        at io.changock.runner.core.MigrationExecutor.processAllChangeLogs(MigrationExecutor.java:83)
        at io.changock.runner.core.MigrationExecutor.lambda$executeMigration$0(MigrationExecutor.java:64)
        at com.github.cloudyrock.mongock.driver.mongodb.springdata.v3.SpringDataMongo3Driver.executeInTransaction(SpringDataMongo3Driver.java:108)
        at io.changock.runner.core.MigrationExecutor.executeInTransactionIfStrategyOrUsualIfNot(MigrationExecutor.java:73)
        at io.changock.runner.core.MigrationExecutor.executeMigration(MigrationExecutor.java:64)
        at io.changock.runner.spring.v5.core.SpringMigrationExecutor.executeMigration(SpringMigrationExecutor.java:38)
        at io.changock.runner.core.ChangockBase.execute(ChangockBase.java:44)
        at io.changock.runner.spring.v5.ChangockSpringBuilderBase$ChangockSpringApplicationRunner.run(ChangockSpringBuilderBase.java:110)
        at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:786)
        at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:776)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:322)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)
        at com.myproject.MyApplication.main(MyApplication.java:23)
Caused by: io.changock.migration.api.exception.ChangockException: Parameter of type [ObjectMapper] must be an interface
        at io.changock.runner.core.DependencyManagerWithContext.getDependency(DependencyManagerWithContext.java:42)
        at io.changock.runner.core.MigrationExecutor.getParameter(MigrationExecutor.java:165)
        at io.changock.runner.core.MigrationExecutor.executeChangeSetMethod(MigrationExecutor.java:155)
        at io.changock.runner.core.MigrationExecutor.executeAndLogChangeSet(MigrationExecutor.java:111)
        at io.changock.runner.spring.v5.core.SpringMigrationExecutor.executeAndLogChangeSet(SpringMigrationExecutor.java:44)
        at io.changock.runner.core.MigrationExecutor.processSingleChangeSet(MigrationExecutor.java:95)
        ... 19 common frames omitted

I need ObjectMapper in my changelogs because my migration process reads json files.我的更改日志中需要 ObjectMapper,因为我的迁移过程读取 json 文件。

As you can see in the Using custom beans in changeSet methods section, in the Mongock's documentation , the custom beans you use in your changeSet must be interfaces.正如您在更改集方法中使用自定义 bean 部分中看到的那样,在 Mongock 的文档中,您在更改集中使用的自定义 bean 必须是接口。

Here there is some explanation. 这里有一些解释。

However, you can tell Mongock you don't want that.但是,您可以告诉 Mongock 您不想要那样。 The less intrusive mechanism is by adding the annotation @NonLockGuarded to you changeSet parameter, as explained in this section .侵入性较小的机制是向您的@NonLockGuarded参数添加注释@NonLockGuarded ,如本节所述

The down side of that approach is that you need to add the annotation every time you use that bean in all your changeSets.这种方法的缺点是每次在所有更改集中使用该 bean 时都需要添加注释。 Maybe a more convenient way, but also more intrusive, is by adding the same annotation to your bean's type, in this case ObjectMapper, as explained in this section .也许更方便但也更具侵入性的方法是向 bean 的类型添加相同的注释,在本例中为 ObjectMapper,如本节所述

As I assume it's the jackson's ObjectMapper, instead all of this, you can take the SpringContext as parameter and get ObjectMapper bean from it, but it's not ideal, for obvious reasons(this applies to any kind of bean)正如我假设它是杰克逊的 ObjectMapper,而不是所有这些,您可以将 SpringContext 作为参数并从中获取 ObjectMapper bean,但由于显而易见的原因,它并不理想(这适用于任何类型的 bean)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM