如果你在 start.spring.io 上使用 Web 和 MongoDB 模块创建一个新的 Spring Boot 项目,添加任何 @Document 注释类,另外将 kotlin.version 从 1.2.10 升级到 1.2.21,你会得到一个 NoClassDefFoundError:
Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.0.0.M7:run (default-cli) on project demo: An exception occurred while running. null: InvocationTargetException: Error creating bean with name 'mongoTemplate' defined in class path resource [org/springframework/boot/autoconfigure/data/mongo/MongoDataAutoConfiguration.class]: Unsatisfied dependency expressed through method 'mongoTemplate' parameter 1;
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'mappingMongoConverter' defined in class path resource [org/springframework/boot/autoconfigure/data/mongo/MongoDataAutoConfiguration.class]: Unsatisfied dependency expressed through method 'mappingMongoConverter' parameter 1; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoMappingContext' defined in class path resource [org/springframework/boot/autoconfigure/data/mongo/MongoDataAutoConfiguration.class]: Invocation of init method failed;
nested exception is java.lang.NoClassDefFoundError: kotlin/reflect/jvm/internal/impl/load/kotlin/reflect/ReflectKotlinClass:
kotlin.reflect.jvm.internal.impl.load.kotlin.reflect.ReflectKotlinClass
当 @Document 注释类被删除时,应用程序启动时没有问题。
从错误消息看来,MongoDataAutoConfiguration 需要 ReflectKotlinClass,它在类路径中找不到。kotlin-reflect 模块在依赖列表中。是否需要任何其他 kotlin 模块?