我对Java和Spring还是很陌生,这是我的第一个Kotlin应用程序,因此我的问题可能是第8层(我)。但是我真的不知道我做错了什么。根据本教程(https://www.baeldung.com/kotlin-mongodb-spring-webflux),此方法应该有效。
我的常规应用设置是Controller-> Service-> Repository(我没有添加控制器,因为这不是问题)。我的问题是工厂无法创建存储库Bean。
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()
}
我不得不将课程和娱乐标记为“开放”。没有它,构建将无法正常工作。这已经与本教程有所不同。
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()
}
}
“ findAll()”方法是从ReactiveMongoRepository继承的
BookingsRepository.kt
interface IBookingsRepository : ReactiveMongoRepository<Booking, String>
该接口显然没有init函数,但在本教程中也没有。所以我认为它应该按原样工作?
有人暗示此设置有什么问题吗?
答案 0 :(得分:0)
这里有几个问题:
您需要向org.jetbrains.kotlin:kotlin-reflect
添加依赖项。如果您在https://start.spring.io/
生成Kotlin项目,则应将其自动添加。
Baeldung教程不好。他们排除了自动配置 没有理由,或者至少没有解释原因。 Spring Boot就是关于 Convention Over Configuration,因此默认应使用 尽可能多的自动配置。
正如您在注释中指出的那样,不排除自动配置会导致异常。这是由于Spring Data MongoDb中的一个错误,该错误是由于DATAMONGO-2355而最近打开了票证this question的。
在这种情况下,正确的解决方案不是排除自动配置,而是更多地使用它。这里没有理由扩展AbstractReactiveMongoConfiguration
。如this answer中所述,您可以通过属性配置数据库名称,并保留所有其他属性的自动配置。如果由于某种原因需要扩展AbstractReactiveMongoConfiguration
,则在修复该错误之前,您可以覆盖reactiveMongoTemplate()
方法,以将返回类型缩小为ReactiveMongoTemplate
,如this answer中所述。 / p>