I am quite new to Java & Spring and this is my first Kotlin app so my problem could be Layer-8 (me). But I really do not know what I make wrong. According to this tutorial (https://www.baeldung.com/kotlin-mongodb-spring-webflux) this should work.
My general app setup is Controller --> Service --> Repository (I did not add the controller since this is not the issue). My problem is that the Repository Bean can not be created by the factory.
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'driveController' defined in file [/Users/sburger/Documents/Dev/Gravity/App Framework/bouncr/build/classes/kotlin/main/de/example/drive/controller/DriveController.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'driveService' defined in file [/Users/sburger/Documents/Dev/Gravity/App Framework/bouncr/build/classes/kotlin/main/de/example/drive/service/DriveService.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'IBookingsRepository': Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: kotlin/reflect/full/KClasses
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:769) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:218) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1325) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1171) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:849) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:877) ~[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) ~[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext.refresh(ReactiveWebServerApplicationContext.java:67) ~[spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) ~[spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) ~[spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) ~[spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) ~[spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) ~[spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
at de.example.AppKt.main(App.kt:14) ~[main/:na]
at de.example.AppKt.main(App.kt) ~[main/:na]
App.kt
@SpringBootApplication(exclude = [MongoReactiveDataAutoConfiguration::class])
open class App
fun main() {
runApplication<App>()
}
MongoConfig.kt
@Configuration
@EnableReactiveMongoRepositories(basePackageClasses = [IBookingsRepository::class])
open class MongoConfig : AbstractReactiveMongoConfiguration() {
override fun getDatabaseName() = "exampledb"
override fun reactiveMongoClient() = mongoClient()
@Bean
override fun reactiveMongoTemplate() = ReactiveMongoTemplate(mongoClient(), databaseName)
@Bean
open fun mongoClient(): MongoClient = MongoClients.create()
}
I had to mark the class and fun as "open". Without it the build did not work. This is already different to the tutorial.
DriveService.kt
interface IDriveService {
fun getBookings(userId: String) : Flux<Booking>
}
@Service
class DriveService @Autowired constructor(private val bookingsRepository: IBookingsRepository) :
IBookingService {
override fun getBookings(userId: String): Flux<Booking> {
return bookingsRepository.findAll()
}
}
The "findAll()" method is inherited from ReactiveMongoRepository
BookingsRepository.kt
interface IBookingsRepository : ReactiveMongoRepository<Booking, String>
This interface has obviously no init function, but in the tutorial it also has none. So I assume it should work as it is?
Has someone a hint what could be wrong with this setup?