I'm writing an SSM project in Kotlin and this happens as long as I try to annotate a class (a service implementation) with @Service.

  1. Tried to code this implementation class in Java instead and it worked all fine.
  2. Tried to keep only the necessary overriding methods.
  3. open is already added.
  4. context:component-scan已配置.

java.lang.IllegalStateException: Failed to load ApplicationContext

at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:125)
at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:107)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:242)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:227)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:246)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'seckillServiceImpl' defined in file [blahblah\target\classes\service\impl\SeckillServiceImpl.class]: Unexpected exception during bean creation; nested exception is java.lang.NoClassDefFoundError: kotlin/reflect/full/KClasses
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:518)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:312)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:756)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:868)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:128)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:60)
at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:109)
at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:246)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:99)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:117)
... 24 more
Caused by: java.lang.NoClassDefFoundError: kotlin/reflect/full/KClasses
at org.springframework.beans.BeanUtils$KotlinDelegate.findPrimaryConstructor(BeanUtils.java:703)
at org.springframework.beans.BeanUtils.findPrimaryConstructor(BeanUtils.java:193)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.determineCandidateConstructors(AutowiredAnnotationBeanPostProcessor.java:286)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineConstructorsFromBeanPostProcessors(AbstractAutowireCapableBeanFactory.java:1197)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1122)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502)
... 37 more
Caused by: java.lang.ClassNotFoundException: kotlin.reflect.full.KClasses
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 44 more

Additional info:

Relative structures

- src
  - kotlin
      - service
        An interface
        - impl
          -> The implementation <-

100

<context:component-scan base-package="service" />

Tried to keep only the necessary methods

@Service
open class SeckillServiceImpl : SeckillService {
    override fun getAll(): List<Seckill> {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun getById(seckillId: Long): Seckill? {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun exportSeckillUrl(seckillId: Long): SeckillUrlExposer {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun doSeckill(seckillId: Long, userPhone: Long, md5: String?): SeckillExecutionResult {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }
}

What's wrong and what should I do? Thanks!

推荐答案

Kotlin's reflection facilities needed by Spring are shipped in a separate JAR. Try adding it into your build:

100:

compile 'org.jetbrains.kotlin:kotlin-reflect:1.1.51'

100:

<dependency>
    <groupId>org.jetbrains.kotlin</groupId>
    <artifactId>kotlin-reflect</artifactId>
    <version>1.1.51</version>
</dependency>

Kotlin相关问答推荐

外键是主键的一部分,但不是索引的一部分.房间

我如何测试一个可组合组件没有显示,但如果它不存在也接受?

Kotlin:类型不匹配:推断的类型已运行,但应等待

mutableStateOf 和 mutableStateListOf 有什么区别?

将 java Optional 转换为 Kotlin Arrow Option

如何在 micronaut 上启用 swagger UI?

在 Kotlin 中 import 如何找到文件路径?

具有泛型类型的 Kotlin 密封接口不会为其子类型推断约束

Kotlin 中私有集的完整语法 struct 是什么?

JobIntentService 被销毁,当应用程序被销毁时

IntelliJ 不会根据 ktlint 的期望对 Kotlin 导入进行排序

作为 Kotlin 中的函数的结果,如何从 Firestore 数据库返回列表?

Jetpack Compose State:修改类属性

如何在 Jetpack Compose 的 LazyColumn/LazyRow 中禁用和启用滚动?

Kotlin 中更好的回调方法是什么?侦听器与高阶函数

这是 Kotlin 中的错误还是我遗漏了什么?

在 Kotlin 中创建非绑定服务

如何将vararg作为数组传递给Kotlin中的函数?

如何根据ArrayList的对象属性值从中获取最小/最大值?

Kotlin - 如果不为空,则使用修改后的 Obj props 覆盖 Obj props