Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot cglib代理的实现接口有什么不同?_Spring Boot_Kotlin_Cglib - Fatal编程技术网

Spring boot cglib代理的实现接口有什么不同?

Spring boot cglib代理的实现接口有什么不同?,spring-boot,kotlin,cglib,Spring Boot,Kotlin,Cglib,我在春季使用ProxyFactory创建了2个代理对象。 一个代理对象已使用接口和一个代理对象未使用接口。 但无法使用jdk动态代理。所有代理对象都使用cglib。 实现接口调用实方法的代理对象。 未实现接口的代理对象具有意外结果。 两个cglib代理对象之间有什么区别? 两者之间的唯一区别是接口 // Not implement interface open class Person: AbstractPerson() { } abstract class AbstractPerson(va

我在春季使用ProxyFactory创建了2个代理对象。 一个代理对象已使用接口和一个代理对象未使用接口。 但无法使用jdk动态代理。所有代理对象都使用cglib。 实现接口调用实方法的代理对象。 未实现接口的代理对象具有意外结果。 两个cglib代理对象之间有什么区别? 两者之间的唯一区别是接口

// Not implement interface
open class Person: AbstractPerson() {
}

abstract class AbstractPerson(var age: Int? = null,
                              var name: String? = null) {
    fun init() {
        this.age = 31
        this.name = "LichKing"
    }

    fun introduce(): String = "age: $age name: $name"
}

kotlin方法的默认选项是
final
。 原因是
引入
方法无法扩展。 当使用界面时,默认选项是
open
,因此可以扩展它

gradle插件
kotlin spring
仅用于spring注释。
它不适用于抽象类。

kotlin方法的默认选项是
final
。 原因是
引入
方法无法扩展。 当使用界面时,默认选项是
open
,因此可以扩展它

gradle插件
kotlin spring
仅用于spring注释。 它不适用于抽象类

// Implement interface
open class PersonImpl: AbstractPersonImpl() {
}

abstract class AbstractPersonImpl(var age: Int? = null,
                                  var name: String? = null): PersonInterface {
    fun init() {
        this.age = 31
        this.name = "LichKing"
    }

    override fun introduce(): String = "age: $age name: $name"
}

interface PersonInterface {
    fun introduce(): String
}
// Test
class PersonTest {
    @Test
    fun implementInterface() {
        val p = PersonImpl()
        p.init()

        val proxyFactory: ProxyFactory = ProxyFactory()

        proxyFactory.setTarget(p)

        val proxy = proxyFactory.proxy as PersonImpl

        println(proxy.javaClass)
        println(proxy.introduce()) // "age: 31 name: LichKing"
    }

    @Test
    fun notImplementInterface() {
        val p = Person()
        p.init()

        val proxyFactory: ProxyFactory = ProxyFactory()

        proxyFactory.setTarget(p)

        val proxy = proxyFactory.proxy as Person

        println(proxy.javaClass)
        println(proxy.introduce()) // "age: null name: null"
    }
}