Loading published curriculum…

09 · Spring Framework and the IoC container / LESSON 43 OF 102

AOP, proxies, and self-invocation

A proxy wraps calls crossing an object boundary to add behavior such as transactions or timing.

Before this lesson: Bean lifecycle and Java configuration

Understand the topic

Spring AOP intercepts eligible method executions through a proxy. A call from one method to another on this stays inside the target and normally bypasses proxy advice. Private methods are not ordinary proxy interception points.

JDK proxies operate through interfaces; class-based proxies subclass the target and have restrictions around final classes or methods. Keep cross-cutting concerns small and avoid aspects that silently change core business meaning.

EXAMPLE · JAVA

ProxyDemo.java

import org.springframework.aop.framework.ProxyFactory;
import org.aopalliance.intercept.MethodInterceptor;
public class ProxyDemo {
 public interface Work { String run(); }
 public static void main(String[] args) {
  Work target = () -> "done";
  var factory = new ProxyFactory(target);
  factory.addAdvice((MethodInterceptor) call -> {
   System.out.println("before"); return call.proceed();
  });
  System.out.println(((Work) factory.getProxy()).run());
 }
}

What to expect

With Spring AOP dependencies, prints before then done. Calling target.run directly bypasses the advice.

YOUR TURN

Put it into practice

  1. 01Compare invoking a target directly with invoking its proxy.
  2. 02Move a cross-cutting operation behind a separate injected service and inspect the call path.
  3. 03Verify: You can explain why internal self-invocation does not cross the proxy boundary.

Pass a quiz or complete a lab to check today’s streak. Each lesson counts once per day, including when you review it later.

UNDER THE HOOD

Build the mental model

A proxy wraps calls crossing an object boundary to add behavior such as transactions or timing.

  1. 01Spring AOP intercepts eligible method executions through a proxy. A call from one method to another on this stays inside the target and normally bypasses proxy advice. Private methods are not ordinary proxy interception points.
  2. 02JDK proxies operate through interfaces; class-based proxies subclass the target and have restrictions around final classes or methods. Keep cross-cutting concerns small and avoid aspects that silently change core business meaning.
  3. 03Trace the example: With Spring AOP dependencies, prints before then done. Calling target.run directly bypasses the advice.

A mistake worth avoiding

An annotation works from a controller but not from another method in the same service.

DEBUGGING ROOM

Something broke. Where do you look?

An annotation works from a controller but not from another method in the same service.

Before revealing the diagnosis, write down your first investigation step.

CHECK YOUR UNDERSTANDING

Does a normal this.method call cross the Spring proxy?

Choose one answer

BUILD IT YOURSELF

Compare invoking a target directly with invoking its proxy.

  1. 1.Compare invoking a target directly with invoking its proxy.
  2. 2.Move a cross-cutting operation behind a separate injected service and inspect the call path.
  3. 3.Reproduce this failure, then repair it: An annotation works from a controller but not from another method in the same service.

Acceptance checklist

Lab completion is your self-report. Knowledge checks do not certify project quality.

RETRIEVE IT FROM MEMORY

Does a normal this.method call cross the Spring proxy?

Revisit this lesson in three days after a successful check or lab.

Ready for the next step?

You can explain why internal self-invocation does not cross the proxy boundary.

Reading is separate from passing a knowledge check or completing a lab.

Go deeper · official references