LoginSignup
0

More than 1 year has passed since last update.

posted at

Kotlin + Spring bootで @Service に @Transactional した時の @Autowired がnullになる時の対処方法

Kotlin + Spring bootのサーバサイドKotlinで@Transactionalを付与したときに@Autowired がnullになってエラーが発生したときの対応方法です。

ここでの問題は、Kotlinではメソッドがデフォルトでfinalであるため、Springがクラスのプロキシを作成できないことです。

o.s.aop.framework.CglibAopProxy: Unable to proxy method [public final int org.mycompany.MyDAO.update(...

classとメソッドに「open」を追記することで問題が解決されます。

@Service
@Transactional  
open class MyDAO(val jdbcTemplate: JdbcTemplate) {

   open fun update(sql: String): Int {
       return jdbcTemplate.update(sql)
   }

} 

これで自分のシステムもうまく動きました。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
0