Understanding the Core Principles of Spring AOP Aspect Programming

This article explains how Spring AOP integrates AspectJ using CGLIB or JDK dynamic proxies, walks through a complete logging aspect example, and details the internal registration, proxy creation, and invocation processes that enable cross‑cutting concerns without modifying business code.

Shepherd Advanced Notes
Shepherd Advanced Notes
Shepherd Advanced Notes
Understanding the Core Principles of Spring AOP Aspect Programming

Spring AOP is one of the two core modules of the Spring framework, alongside Spring IoC. It implements Aspect‑Oriented Programming, which abstracts cross‑cutting concerns horizontally, allowing developers to separate concerns such as logging, monitoring, and security from business logic.

The module achieves runtime method enhancement by using CGLIB or JDK dynamic proxies. By weaving advice into bean methods, Spring AOP reduces coupling and improves reusability and development efficiency.

An example aspect class LogAspect is defined with @Aspect and a pointcut expression execution(* com.shepherd.aop.service.*.*(..)). It contains five advice methods—@Before, @AfterReturning, @AfterThrowing, @After, and @Around—demonstrating the execution order and printing log messages before, after, on exception, and around the target method.

A simple service interface MyService and its implementation MyServiceImpl provide a business method. A configuration class annotated with @Configuration, @ComponentScan, and @EnableAspectJAutoProxy enables AOP proxying and scans the component packages. The main method obtains the service bean from an AnnotationConfigApplicationContext and invokes the method, producing the logged output shown in the article.

The @EnableAspectJAutoProxy annotation is defined with attributes proxyTargetClass and exposeProxy, and it imports AspectJAutoProxyRegistrar. This registrar implements ImportBeanDefinitionRegistrar and registers AnnotationAwareAspectJAutoProxyCreator into the container. AnnotationAwareAspectJAutoProxyCreator is a BeanPostProcessor. During bean initialization, its postProcessAfterInitialization method checks whether the bean is already in earlyProxyReferences. If not, it calls wrapIfNecessary to decide whether to create a proxy. wrapIfNecessary obtains specific interceptors via getAdvicesAndAdvisorsForBean. When advice is present, it records the bean as advised and calls createProxy with a ProxyFactory. The factory determines whether to use JDK dynamic proxies or CGLIB based on the proxyTargetClass flag and the bean’s class hierarchy.

Inside createProxy, an advisor chain is built, the ExposeInvocationInterceptor is added, and the chain is sorted. The proxy is then created by calling proxyFactory.getProxy, which delegates to either JdkDynamicAopProxy or ObjenesisCglibAopProxy. JdkDynamicAopProxy implements InvocationHandler. Its invoke method retrieves the target object, obtains the interceptor chain for the invoked method, and either directly invokes the method (if the chain is empty) or creates a ReflectiveMethodInvocation to proceed through the chain. It also handles proxy exposure, argument adaptation, and return‑value adjustments.

Through this mechanism, Spring AOP enables cross‑cutting concerns such as logging, performance monitoring, exception handling, and transaction management to be added without changing the original source code, improving modularity, maintainability, and extensibility of applications.

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.

JavaAOPSpringAspectJDynamic ProxyCGLIB
Shepherd Advanced Notes
Written by

Shepherd Advanced Notes

Dedicated to sharing advanced Java technical insights, daily work snippets, and the power of persistent effort.

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.