Backend Development 16 min read

Implementing AOP in a Custom Spring‑like Framework

This article explains the principles and step‑by‑step implementation of Aspect‑Oriented Programming (AOP) in a lightweight Spring‑style container, covering core concepts, advice and pointcut design, weaving mechanisms, proxy creation, observer integration, and practical code examples.

Top Architect
Top Architect
Top Architect
Implementing AOP in a Custom Spring‑like Framework

So far we have built a simple IoC and DI container; now we turn to the more challenging topic of Aspect‑Oriented Programming (AOP). AOP allows adding cross‑cutting behavior such as logging without modifying the original class code.

Key AOP concepts include advice (the code to run), pointcut (where to apply it), aspect (advice + pointcut), join point (a selectable execution point) and weaving (the process of applying aspects).

We analyze how advice should be defined and registered, noting that users provide concrete advice implementations which the framework discovers via interfaces similar to JDBC drivers. The typical advice lifecycles (Before, After, After‑returning, After‑throwing, Around) are listed.

For pointcut handling we adopt a simple regular‑expression parser to match method signatures, illustrated by the following pattern syntax:

execution(modifiers-pattern? ret-type-pattern declaring-type-pattern?name-pattern(param-pattern) throws-pattern?)

We discuss how the framework determines which beans need enhancement, using a BeanPostProcessor that decides between JDK dynamic proxies and CGLIB proxies. The observer pattern is introduced to keep the BeanFactory code clean: the factory acts as the subject and BeanPostProcessor implementations act as observers.

To support multiple advice types without invoking the target method repeatedly, we implement a recursive chain (responsibility‑chain pattern). The core of this chain is shown below:

public class AopAdviceChain {
    private Method nextMethod;
    private Method method;
    private Object target;
    private Object[] args;
    private Object proxy;
    private List<Advice> advices;
    private int index = 0;
    public AopAdviceChain(Method method, Object target, Object[] args, Object proxy, List<Advice> advices) { ... }
    public Object invoke() throws InvocationTargetException, IllegalAccessException {
        if (index < advices.size()) {
            Advice advice = advices.get(index++);
            if (advice instanceof BeforeAdvice) {
                ((BeforeAdvice) advice).before(method, args, target);
            } else if (advice instanceof AroundAdvice) {
                return ((AroundAdvice) advice).around(nextMethod, null, this);
            } else if (advice instanceof AfterAdvice) {
                Object res = this.invoke();
                ((AfterAdvice) advice).after(method, args, target, res);
                return res;
            }
            return this.invoke();
        } else {
            return method.invoke(target, args);
        }
    }
}

The chain ensures that around and after advice are executed in the correct order without invoking the target method multiple times.

We also provide façade interfaces (e.g., BeanFactoryAware) so that framework components can obtain container references without tight coupling, mirroring Spring’s approach.

All source code for the full implementation is hosted on GitHub:

https://github.com/lliyu/myspring

In summary, the article walks through a complete AOP implementation, touching on many design patterns such as factory, façade, observer, and responsibility chain, and demonstrates how AOP integrates tightly with the earlier IoC/DI layers.

Design PatternsJavaproxyaopSpringdependency injectionAspect-Oriented Programming
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.