Backend Development 7 min read

Mastering Spring AOP: Dynamic Proxy, Java Agent, and Maven Plugin Techniques

Explore three Spring AOP implementation methods—dynamic proxy, Java Agent, and AspectJ Maven plugin—by setting up a sample Service, defining aspects, configuring aop.xml and JVM arguments, and comparing their flexibility, performance impact, and compile‑time weaving within a Spring Boot 2.7 environment.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Mastering Spring AOP: Dynamic Proxy, Java Agent, and Maven Plugin Techniques

1. Introduction Spring enhances code functionality through mechanisms like AOP, using dynamic proxies, Java agents, or compile‑time weaving to add cross‑cutting concerns without modifying source code.

2. Practical Cases

2.1 Prepare Environment

<code>@Service
public class UserService {
  public void save() {
    System.out.println("save...");
  }
}
</code>

The following examples operate on this Service.

Run Test Class

<code>SpringApplication app = new SpringApplication(AppApplication.class);
app.setWebApplicationType(WebApplicationType.NONE);
ConfigurableApplicationContext context = app.run(args);
UserService us = context.getBean(UserService.class);
us.save();
</code>

2.2 Proxy Method

This is the most common approach: define an aspect and let Spring create a dynamic proxy at runtime.

<code>@Component
@Aspect
public class LogAspect {
  @Pointcut("execution(* com.pack..*Service.*(..))")
  private void log() {}

  @Before("log()")
  public void before(JoinPoint jp) {
    System.out.println("before, " + jp.getSignature());
  }
}
</code>

Running the test prints:

<code>before, void com.pack.aop.agent.UserService.save()
save...
</code>

Only the @Aspect bean is required for enhancement.

2.3 Agent Enhancement

Using a Java Agent intercepts class loading at the JVM level, supporting broader AOP scenarios with higher performance potential.

Define aop.xml in META-INF :

<code>&lt;aspectj&gt;
  &lt;weaver&gt;
    &lt;!-- Classes to weave --&gt;
    &lt;include within="com.pack.aop.agent..*" /&gt;
  &lt;/weaver&gt;
  &lt;aspects&gt;
    &lt;aspect name="com.pack.aop.agent.LogAspect" /&gt;
  &lt;/aspects&gt;
&lt;/aspectj&gt;
</code>

Add the JVM argument:

<code>-javaagent:D:/maven/org/aspectj/aspectjweaver/1.9.7/aspectjweaver-1.9.7.jar
</code>

Running the test shows the business code executed while the UserService class itself is not proxied (see images).

Decompiled bytecode confirms no changes; the agent weaves at load time.

2.4 Compile‑time Plugin Method

AspectJ Maven plugin modifies bytecode during compilation, offering the best runtime performance.

<code>&lt;plugin&gt;
  &lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt;
  &lt;artifactId&gt;aspectj-maven-plugin&lt;/artifactId&gt;
  &lt;version&gt;1.11&lt;/version&gt;
  &lt;configuration&gt;
    &lt;complianceLevel&gt;1.8&lt;/complianceLevel&gt;
    &lt;source&gt;1.8&lt;/source&gt;
    &lt;target&gt;1.8&lt;/target&gt;
    &lt;showWeaveInfo&gt;true&lt;/showWeaveInfo&gt;
    &lt;Xlint&gt;ignore&lt;/Xlint&gt;
    &lt;encoding&gt;UTF-8&lt;/encoding&gt;
    &lt;skip&gt;true&lt;/skip&gt;
  &lt;/configuration&gt;
  &lt;executions&gt;
    &lt;execution&gt;
      &lt;configuration&gt;
        &lt;skip&gt;false&lt;/skip&gt;
      &lt;/configuration&gt;
      &lt;goals&gt;
        &lt;goal&gt;compile&lt;/goal&gt;
      &lt;/goals&gt;
    &lt;/execution&gt;
  &lt;/executions&gt;
&lt;/plugin&gt;
</code>

Remove the @Component annotation from the aspect, recompile with mvn clean compile , and run the test again. The output shows the code is enhanced without a proxy, confirming compile‑time weaving.

Decompiling the class reveals the aspect code already woven, explaining the superior performance of this method.

Summary

Dynamic proxy offers flexibility and lightweight runtime weaving; Java Agent provides broad AOP coverage with high performance potential but requires more configuration; AspectJ Maven plugin performs compile‑time bytecode modification, eliminating runtime overhead while supporting complex logic.

aopSpring BootAspectJDynamic Proxyjava agentMaven plugin
Spring Full-Stack Practical Cases
Written by

Spring Full-Stack Practical Cases

Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.

0 followers
Reader feedback

How this landed with the community

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