So I'm upgrading from Spring 3.2 to 3.4 In Spring...
# spring
p
So I'm upgrading from Spring 3.2 to 3.4 In Spring 3.2 this way of registering an initializer to kick off registering beans for the kotlin beans DSL is deprecated:
Copy code
context.initializer.classes: mypackage.BeansInitializer
So instead I've used META-INF/spring.factories:
Copy code
org.springframework.context.ApplicationContextInitializer=\
 mypackage.BeansInitializer
and my application breaks. The issue is that in my BeansInitializer class I'm using ref() to inject a bean that is created via an @Component annotation in a jar file. The trouble is that now the initalizer seems to run before it's dependent bean is created, so I get a message like this: A component required a bean of type 'Whatever' that could not be found. How do correctly use the kotlin beans DSL in Spring 3.4.5 ? NB I can't modify the main function to add the initializer that way as that only works for the main application, not tests, and I don't want to add the initializer to loads of integration tests either.
If anyone else ever gets this, the solution is to put the spring.factories file in your test resource as well.
h
Copy code
fun main(args: Array<String>) {
    runApplication<DemoApplication>(*args) {
        addInitializers(beans())
    }
}
Check my example https://github.com/hantsy/spring-reactive-sample/blob/master/boot-kotlin-dsl/src/main/kotlin/com/example/demo/DemoApplication.kt
p
addInitializers(beans()) only works for the main application, so won't work for any tests. Also, if any bean imports another bean via a ref(), this simply no longer works. We ended up converting all our beans DSL code back to old fashioned
@Bean
annotations, which was tortuous. Hopefully the new beans DSL in Spring 7 will be worth waiting for.
h
@Paul N The sample project includes testing codes,