Skip to content
Ameen Abozaid
Planned lessonMID-009-L05

Dynamic proxies

  • Lesson 5 of 6
  • Mid-level
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.Proxy creates an object implementing interfaces; each call goes to an InvocationHandler.
  • 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 this bypasses it, so @Transactional self-invocation does not start a transaction.
  • Fix: move the method to another bean, or inject the proxy.
Draft outline details

Demonstration

java
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.

Lab and exercises Quiz

Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.

Continue to next lesson

Type to search levels, phases, modules and lessons.