mongock / mongock

Lightweight Java based migration tool

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Injecting beans by name

thorstenvogel opened this issue · comments

The getBean-Methods in io.mongock.runner.spring.base.context.SpringDependencyContext swallow Exceptions, if either beans are not defined or there are multiple instances of beans available (org.springframework.beans.factory.NoUniqueBeanDefinitionException). I only found that out by debugging. It would have helped if the error would have been logged.

How is it possible to inject a bean by name into a ChangeUnit using Spring Boot Runner?

Hello @thorstenvogel ,

can you tell me which version are you using, please?

Hi @dieppa, using latest and greatest 5.3.4 :)

I managed to get it to work by defining my own MongockApplicationRunner using MongockSpringboot.builder() and registering the dependencies manually by name.

So i am missing out of the configuration magic that @EnableMongock provides.

My goal was to inject dependencies via their name into ChangeUnits - and i couldn't figure out another way.

EDIT:
My approach does not work, so the question remains.

I managed to inject my named beans into ChangeUnit-Methods via

    @Execution
    public void migrateSomething(@NonLockGuarded
                                                      @Named("namedBean")
                                                      MyBean namedBean) {
        // do something with namedBean
    }

The injection mechanism in Mongock does not allow fields to be injected into the class as constructor-parameter as far as i can see.

For the future ist would be great if ChangeUnits could just be Spring beans, utilizing the dependency-injection mechanisms of Spring.

Hello, I didn't answer until now as we have been pretty busy and I saw you managed to make it work.

Your suggestion is very interesting, but represents important challenges, specially because Mongock proxies the injection for distributed synchronisation's sake.

On the other hand, I don't think it's that far. As long as I remember, springboot supports @Named injections and Mongock should support dependency injection as construction time.

Thanks!