Chapter 2: JDBC
Explicit Transaction Handling
JDBCで、setAutoCommit(false)を明示的にすることでトランザクション境界を制御するが、Spring BootなどのFWでは、Controller In/Outでそのようになっているのか
@Transactional
の宣言的アノテーションを使うと、そのメソッドがトランザクション境界となる
この
@Transactional
のようなアノテーションの仕組み(AOPの実装)は?実行時にclass fileを書き換える、作成する といった黒魔術的な技術によって支えられている
- ASM
- byte code parser
jacoco
などのCode coverage取得など、色々使われている
- Byte Buddy - runtime code generation for the Java virtual machine
- ASM
Javaのclass fileはフォーマットがかっちりしているので、ごにょごにょできるわけだ
- Chapter 4. The class File Format
- みると結構勉強になる
- Chapter 4. The class File Format
黒魔術は危険な側面もあるが、便利な側面もある
However, this picture changes when creating libraries that need to interact with arbitrary code and unknown type hierarchies. In this context, a library implementer must often choose between either requiring a user to implement library-proprietary interfaces or to generate code at runtime when the user’s type hierarchy becomes first known to the library.
– Byte Buddy - runtime code generation for the Java virtual machine
なるほど。インタフェースを実装してもらうか、ランタイムに情報を集めるしか無い。
黒魔術によって、POJO化できる。
が、ソースコードを追ってるとき、裏で何をやってるかわけわからん みたいなことにはなる。Spring Frameworkでは、
Proxy
という仕組みと呼ばれており、そのインスタンスが実際には実行され、周辺処理が挟み込みされる.AspectJ
参考