web-dev-qa-db-ja.com

Kotlinで@Serviceを使用すると例外がスローされます

KotlinでSSMプロジェクトを作成していますが、これは、クラス(サービス実装)に@Serviceで注釈を付けようとしている限り発生します。

  1. 代わりにJavaでこの実装クラスをコーディングしようとしましたが、すべて正常に機能しました。
  2. 必要なオーバーライドメソッドのみを保持しようとしました。
  3. openはすでに追加されています。
  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

追加情報:

相対構造

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

spring-service.xml

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

必要なメソッドのみを保持しようとしました

@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.
    }
}

何が問題で、どうすればよいですか?ありがとう!

11
Zenas Chen

Kotlinの リフレクション機能 Springに必要なものは、別のJARで出荷されます。ビルドに追加してみてください。

build.gradle

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

pom.xml

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