Kotlin编写Spring Boot,在业务层中使用@Transactional+@Autowired时出现如下错误:

lateinit property dao has not been initialized

@Transactional+@Autowired出现的lateinit property xx has not been initialized错误

出错代码如下:

@Transactional+@Autowired出现的lateinit property xx has not been initialized错误

2 解决办法

因为Kotlin类默认是final的,加上@Transactional后编译器提示需要open

@Transactional+@Autowired出现的lateinit property xx has not been initialized错误

但是加上open后没用,因此把@Transactional去掉后发现不会报错:

@Transactional+@Autowired出现的lateinit property xx has not been initialized错误

因此怀疑是@Transactional的问题,因为需要在类上加上open,所以尝试性地在对应的方法上面也加上open

@Transactional+@Autowired出现的lateinit property xx has not been initialized错误

问题得到解决。

3 原因

因为@Transactional实际上是通过反射获取Bean的注解信息,利用AOP实现的,而在Kotlin中使用AOP需要在类或方法上加上open

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-16
  • 2022-12-23
  • 2021-06-01
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-29
相关资源
相似解决方案