Dynamic proxies
On This Lesson 4 topics
This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.
In this lesson
explain how Spring adds behavior around beans.
Planned topics
java.lang.reflect.Proxycreates an object implementing interfaces; each call goes to anInvocationHandler.- The handler can add behavior (timing, transactions) and call the real object.
- Spring uses JDK proxies for interfaces and CGLIB subclass proxies for classes (verify details for your Spring version).
- Only calls through the proxy are intercepted. A call from one method to another on
thisbypasses it, so@Transactionalself-invocation does not start a transaction. - Fix: move the method to another bean, or inject the proxy.
Draft outline details
Demonstration
Svc proxy = (Svc) Proxy.newProxyInstance(loader, new Class<?>[]{Svc.class},
(p, m, args) -> { log(m); return m.invoke(target, args); });Check your understanding
Why does this.save() skip @Transactional?
Show answer
this is the raw object, not the proxy.
Practice
Apply this lesson with the module's lab, exercises and quiz.
Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.