Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

AOP Implementation Using CGLIB Subclass Proxies

Tech Oct 30 1

Core Concepts of Aspect-Oriented Programming

Aspect-Oriented Programming (AOP) extracts cross-cutting concerns from business logic. It targets specific steps or phases in a process, achieving low coupling between logical components.

A proxy acts as a surrogate or placeholder, controlling access to another object. It allows adding extra behavior before or after the target object's execution, effectively extending its functionality.

CGLIB-Based Subclass Proxies

CGLIB generates proxy objects by creating a subclass of the target class at runtime. This approach overcomes a key limitation of JDK dynamic proxies, which require the target to implement at least one interface. CGLLIB proxies classes directly using bytecode manipulation powered by the ASM framework. Major AOP frameworks such as Spring leevrage it to enable method interception.

Several constraints apply:

  • The target class cannot be declared final.
  • Methods marked final or static will not be intercepted.
  • Include the CGLIB library; Spring's core module packages it, so adding spring-core is sufficient.

Implementation Example

Consider a plain service class that acts as the intercept target:

package com.example.aop.cglib;

public class AccountService {

    public void processTransaction() {
        System.out.println("Processing transaction...");
    }
}

Define a proxy factory that implements Spring's MethodInterceptor:

package com.example.aop.cglib;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;
import java.lang.reflect.Method;

/*
 * Subclass proxy factory using CGLIB.
 * Intercepts method calls to add behavior.
 */
public class ServiceProxyInterceptor implements MethodInterceptor {

    private final Object delegate;

    public ServiceProxyInterceptor(Object delegate) {
        this.delegate = delegate;
    }

    /*
     * Creates a dynamically generated subclass proxy instance.
     */
    public Object createProxy() {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(delegate.getClass());
        enhancer.setCallback(this);
        return enhancer.create();
    }

    @Override
    public Object intercept(Object enhancedInstance, Method method, Object[] arguments,
                            MethodProxy methodProxy) throws Throwable {
        System.out.println(">>> Interception start");

        Object result = methodProxy.invokeSuper(enhancedInstance, arguments);

        System.out.println("<<< Interception end");
        return result;
    }
}

After execution, the returned reference is no longer the original AccountService but an enhanced CGLIB-generated subclass instance. The console output confirms that the custom logic runs before and after the target method.

Choosing Between JDK and CGLIB Proxies in Spring AOP

Spring's default strategy is straightforward:

  • If the target bean implements an interface, the JDK dynamic proxy is used.
  • If no interface is implemented, CGLIB subclass proxy is applied automatically.
Tags: AOPCGLIB

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.