javaspringspring-bootmongock

Mongock spring-boot delay bean creation until Mongock is done


With liquibase and Spring Boot I can do @DependsOn("liquibase") to wait when I have a @Bean creating method. This lets me create beans that depend on initialization data.

How do I do this in mongock?

I saw this question but I need to order the bean creation in the context, not force the context to wait on the mongock init to happen before being complete...


Solution

  • Turns out to do this we can't use the annotation configuration, we need to use the Mongock initializing bean and create it in a @Bean, name it, and then we can use DependsOn('mongock') for dependent beans:

    Note that this is Kotlin...

    @Configuration
    class MongockConfiguration {
        @Bean("mongock")
        fun mongockInitializingBeanRunner(
            mongoTemplate: MongoTemplate,
            applicationContext: ApplicationContext
        ): MongockInitializingBeanRunner {
            return MongockSpringboot.builder()
                .setDriver(SpringDataMongoV4Driver.withDefaultLock(mongoTemplate))
                .addMigrationScanPackage("come.package.changelog")
                .setSpringContext(applicationContext)
                .buildInitializingBeanRunner();
        }
    }