Resolving Spring AOP Self‑Invocation Issues by Using Proxy Calls

This article explains why Spring AOP advice does not trigger when a method calls itself via this, demonstrates how to reproduce the problem, and provides a solution using AopContext.currentProxy() together with proper proxy configuration to ensure the aspect executes correctly.

Full-Stack Internet Architecture
Full-Stack Internet Architecture
Full-Stack Internet Architecture
Resolving Spring AOP Self‑Invocation Issues by Using Proxy Calls

In a previous post the author introduced a custom annotation combined with Spring AOP to improve code elegance, but some readers reported that the aspect did not work after configuring Spring AOP.

The author reproduces the issue: a method annotated with @StrategyCache (loadFactor) is invoked internally from another method (getFactor) using this.loadFactor(...), and the advice never runs.

Analysis shows that Spring AOP works only when the target method is invoked through a proxy object. Direct self‑invocation (using this) bypasses the proxy, so the aspect is not applied. Three invocation styles are described: (1) internal call via this, (2) external call on a regular bean instance, and (3) external call on a proxy instance.

To fix the problem, the author modifies the service code to obtain the proxy via AopContext.currentProxy() and invoke the target method through that proxy. The relevant code snippet is:

@Component
public class StrategyService {
    public PricingResponse getFactor(Map<String, String> pricingParams) {
        if (AopContext.currentProxy() instanceof StrategyService) {
            return ((StrategyService) AopContext.currentProxy()).loadFactor(pricingParams);
        } else {
            return loadFactor(pricingParams);
        }
    }

    @StrategyCache(keyName = "key0001", expireTime = 60 * 60 * 2)
    private PricingResponse loadFactor(Map<String, String> pricingParams) {
        // method logic
    }
}

Additionally, the AOP configuration must expose the proxy. For XML configuration:

<aop:aspectj-autoproxy proxy-target-class="true" expose-proxy="true"/>

For Spring Boot, enable proxy exposure with:

@EnableAspectJAutoProxy(exposeProxy = true)
public class Application { }

The article concludes that the self‑invocation issue is serious because it can silently disable transaction management, logging, caching, etc., and recommends checking code for such patterns.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendJavaProxyaopspringCustom Annotation
Full-Stack Internet Architecture
Written by

Full-Stack Internet Architecture

Introducing full-stack Internet architecture technologies centered on Java

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.