微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Spring Proxy Class和Kotlin中的空指针异常

我和kotlin一起面临春天的问题.

我有一个控制器bean(没有接口btw),它通过主构造函数一个自动连接的服务bean.

除非我为控制器使用缓存注释,否则它工作正常.显然,spring缓存会在引擎盖下生成一个代理类来处理缓存.

我的代码看起来像这样:

@RestController
@RequestMapping("/regions/")
open class RegionController @Autowired constructor(val service: RegionService) {
    @RequestMapping("{id}",method = arrayOf(RequestMethod.GET))
    @Cacheable(cacheNames = arrayOf("regions"))
    fun get(@PathVariable id: Long): RegionResource {
        return this.service.get(id)
    }
}

现在问题是在执行方法时出现空指针异常,实际上this.service为null,这在技术上是不可能的,因为它是kotlin中的非空变量.

我假设class proxies generated by spring用null值而不是autowired bean初始化类.这必须是使用kotlin和spring的常见陷阱.你是怎么绕过这个问题的?

最佳答案
在Kotlin中认为classes and members are final.

对于代理库(CGLIB,javaassist)能够代理方法,必须将其声明为非final和非final类(since those libraries implement proxying by subclassing).将您的控制器方法更改为:

@RequestMapping("{id}",method = arrayOf(RequestMethod.GET))
@Cacheable(cacheNames = arrayOf("regions"))
open fun get(@PathVariable id: Long): RegionResource {
    return this.service.get(id)
}

您可能会在控制台中看到有关RegionController方法不受代理约束的警告.

The Kotlin compiler plugin

Kotlin团队承认了这一困难并创建了一个插件,标志着标准的AOP代理候选者,例如: @Component打开.

您可以在build.gradle中启用插件

plugins {
  id "org.jetbrains.kotlin.plugin.spring" version "1.1.60"
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐