Backend Development 9 min read

Understanding AOP: AspectJ, Spring AOP, and CGLIB – A Practical Guide

This article explains the fundamentals of Aspect‑Oriented Programming, introduces AspectJ as a standalone AOP solution, demonstrates how to implement a HelloWorld example with compile‑time weaving, compares it with Spring AOP’s runtime proxies, and shows how CGLIB can be used for proxy generation.

Top Architect
Top Architect
Top Architect
Understanding AOP: AspectJ, Spring AOP, and CGLIB – A Practical Guide

Aspect‑Oriented Programming (AOP) complements object‑oriented programming by handling cross‑cutting concerns such as transaction management, security checks, caching, and object‑pool management.

AOP works by creating proxies, which can be static (compile‑time) or dynamic (runtime). Static proxies are generated during compilation, while dynamic proxies are created at runtime using JDK dynamic proxies or CGLIB.

AspectJ

AspectJ is an independent AOP solution, not part of Spring. It provides its own compiler and weaver for compile‑time weaving.

1. AspectJ Installation

bin: contains commands like ajc (similar to javac ) doc: documentation, reference manuals, API docs lib: core AspectJ JAR files

2. AspectJ HelloWorld Implementation

Business component SayHelloService :

package com.ywsc.fenfenzhong.aspectj.learn;
public class SayHelloService {
    public void say(){
        System.out.print("Hello  AspectJ");
    }
}

Logging aspect LogAspect for after‑method execution:

package com.ywsc.fenfenzhong.aspectj.learn;
public aspect LogAspect{
    pointcut logPointcut():execution(void SayHelloService.say());
    after():logPointcut(){
        System.out.println("记录日志 ...");
    }
}

Compile and run:

Execute ajc -d . SayHelloService.java LogAspect.java

Generates SayHelloService.class

Run java SayHelloService

Output: Hello AspectJ 记录日志 ...

The ajc compiler recognizes AspectJ syntax and weaves the logging code into the class during compilation, demonstrating compile‑time enhancement.

Spring AOP

Spring AOP also creates proxies but does so at runtime, using JDK dynamic proxies or CGLIB. It relies on @AspectJ annotations for defining aspects, pointcuts, and advice, but does not use the AspectJ compiler.

To enable Spring’s @AspectJ support, add <aop:aspectj-autoproxy/> to the configuration.

Example with Spring:

package com.ywsc.fenfenzhong.aspectj.learn;
import org.springframework.stereotype.Component;
@Component
public class SayHelloService {
    public void say(){
        System.out.print("Hello  AspectJ");
    }
}
package com.ywsc.fenfenzhong.aspectj.learn;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class LogAspect {
    @After("execution(* com.ywsc.fenfenzhong.aspectj.learn.SayHelloService.*(..))")
    public void log(){
        System.out.println("记录日志 ...");
    }
}

Running the Spring‑managed bean prints the same output as the AspectJ example.

Summary

AOP proxy = original business class + enhancement logic, generated and managed by the Spring IoC container, which can inject other beans as needed.

Key steps to build an AOP‑enhanced application:

Define a regular business component.

Define a pointcut that may match multiple components.

Define advice that is woven into the component by the AOP framework.

CGLIB

CGLIB (Code Generation Library) creates subclasses at runtime to implement proxies without requiring interfaces, using the ASM bytecode manipulation framework.

To force Spring AOP to use CGLIB, configure <aop:aspectj-autoproxy proxy-target-class="true"/> .

backendJavaAOPSpringAspectJCglib
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

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.