是什么导致Spring boot 1.5.9和2.0.2之间的这种不一致,以及如何修复它?

时间:2018-05-17 11:56:05

标签: spring spring-boot kotlin

当Spring尝试使用公共接口私有实现连接bean时,使用Spring Boot 2.0.2时会引发以下异常:

Caused by: java.lang.IllegalAccessException: Class         
kotlin.reflect.jvm.internal.FunctionCaller$Constructor can not access a     
member of class com.example.demo.MyCompImpl with modifiers "public"
    at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:102) ~[na:1.8.0_112]
    at java.lang.reflect.AccessibleObject.slowCheckMemberAccess(AccessibleObject.java:296) ~[na:1.8.0_112]
    at java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:288) ~[na:1.8.0_112]
    at java.lang.reflect.Constructor.newInstance(Constructor.java:413) ~[na:1.8.0_112]
    at kotlin.reflect.jvm.internal.FunctionCaller$Constructor.call(FunctionCaller.kt:66) ~[kotlin-reflect-1.2.41.jar:1.2.41-release-74 (1.2.41)]
    at kotlin.reflect.jvm.internal.KCallableImpl.call(KCallableImpl.kt:107) ~[kotlin-reflect-1.2.41.jar:1.2.41-release-74 (1.2.41)]
    ... 22 common frames omitted

这适用于Spring Boot 1.5.9。

下面是一个代码示例,有关完整的工作示例,请参阅https://github.com/owyke/spring-private-components

package com.example.demo

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.builder.SpringApplicationBuilder
import org.springframework.stereotype.Component

@SpringBootApplication
class DemoApplication

fun main(args: Array<String>) {
    SpringApplicationBuilder(DemoApplication::class.java).run(*args)
}

interface ValueProvider {
    fun provideValue(): Double
}

@Component
private class ValueProviderImpl(): ValueProvider {
    override fun provideValue() = 3.1415
}

interface MyComp {
    fun getValue(): Double
}

@Component
private class MyCompImpl(val provider: ValueProvider): MyComp {
    override fun getValue() = provider.provideValue()
}

0 个答案:

没有答案