spring-projects-experimental/spring-fu

KoFu: Allow sharing bean definitions with a @SpringBootApplication

Closed this issue · 1 comments

We are using a traditional @SpringBootApplication for production and would like to use KoFu for integration tests. We expect faster tests and would like to learn KoFu already until it is ready for production.

For this, we like to share our bean definitions

val moduleBeans = beans {
    bean<MyHandler>()
    bean<MyService>()
    bean(::routes)
}

between production

@SpringBootApplication
class MyApplication

fun main() {
    runApplication<MyApplication> {
        addInitializers(moduleBeans)
    }
}

and the KoFu app for integration tests

val app = reactiveWebApplication {
    webFlux {
        [...]
    }

    // we would like to include moduleBeans here without code duplication
}

We have found only one way to make sharing the stand-alone bean definitions work:

fun BeanDefinitionDsl.moduleBeans() {
    bean<MyHandler>()
    bean<MyService>()
    bean(::routes)
}
fun main() {
    runApplication<MyApplication> {
        addInitializers(beans { moduleBeans() })
    }
}

and in the DSL

val app = reactiveWebApplication {
    webFlux {
        [...]
    }

    beans {
        moduleBeans()
    }
}

That's not very elegant. Did we miss something? If not, how about adding

fun enable(beans: BeanDefinitionDsl) {
    beans.initialize(context)
}

or even

fun enable(beans: ApplicationContextInitializer<in GenericApplicationContext>) {
    beans.initialize(context)
}

to ConfigurationDsl so we can simply enable the bean definition

val app = reactiveWebApplication {
    webFlux {
        [...]
    }

    enable(moduleBeans)
}

? This also allows a step-by-step migration from a SpringBootApplication by starting with programmatic bean registration.

I agree that we should add this enable in Kofu ConfigurationDsl since it's available in Jafu.