Understanding Spring AOP vs AspectJ: Core Concepts and Configuration

This article compares AspectJ and Spring AOP, explains the four types of Spring AOP support, details classic Spring AOP proxy creation, demonstrates various advice implementations, shows XML and annotation configurations, and introduces auto‑proxy creators for simplifying AOP setup in Java backend development.

Java High-Performance Architecture
Java High-Performance Architecture
Java High-Performance Architecture
Understanding Spring AOP vs AspectJ: Core Concepts and Configuration

Difference

AspectJ

AspectJ is an aspect‑oriented framework that extends the Java language. It defines AOP syntax and provides a dedicated compiler that generates class files compliant with Java bytecode specifications.

Spring AOP

Spring provides four types of AOP support:

Classic Spring AOP

Pure POJO aspects

@AspectJ annotation‑driven aspects

AspectJ‑based aspects (using the AspectJ framework to implement AOP programming)

Classic Spring AOP

It uses ProxyFactoryBean to create proxies.

The types of advice are:

Before advice: org.springframework.aop.MethodBeforeAdvice After returning advice: org.springframework.aop.AfterReturningAdvice Around advice: org.aopalliance.intercept.MethodInterceptor Throws advice:

org.springframework.aop.ThrowsAdvice
public interface IBookDao {
    public int add();
    public int delete();
}

public class BookDaoImpl implements IBookDao {
    public int add() {
        System.out.println("正在添加图书...");
        return 0;
    }
    public int delete() {
        System.out.println("正在删除图书...");
        return 0;
    }
}

// Around advice implementation
public class MyAdvice implements MethodInterceptor {
    public Object invoke(MethodInvocation invocation) throws Throwable {
        System.out.println("Around Advice before method invocation");
        Object o = invocation.proceed();
        System.out.println("Around Advice after method invocation");
        return o;
    }
}

Each join point is treated as a pointcut, intercepting every method.

<bean id="bookDao" class="com.njust.learning.spring.service.BookDaoImpl"/>

<bean id="myadvice" class="com.njust.learning.spring.aop.MyAdvice"/>

<bean id="bookDaoProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
    <property name="target" ref="bookDao"/>
    <property name="proxyInterfaces" value="com.njust.learning.spring.service.IBookDao"/>
    <property name="interceptorNames" value="myadvice"/>
</bean>

Using RegexMethodPointcutAdvisor to intercept specific methods:

<bean id="bookDao" class="com.njust.learning.spring.service.BookDaoImpl"/>

<bean id="myadvice" class="com.njust.learning.spring.aop.MyAdvice"/>

<bean id="rmpAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
    <property name="patterns" value=".*add"/>
    <property name="advice" ref="myadvice"/>
</bean>

<bean id="bookDaoProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
    <property name="target" ref="bookDao"/>
    <property name="proxyInterfaces" value="com.njust.learning.spring.service.IBookDao"/>
    <property name="interceptorNames" value="rmpAdvisor"/>
</bean>

Defining a BeanNameAutoProxyCreator to automatically create proxies for beans matching a name pattern:

<bean id="bookDao" class="com.njust.learning.spring.service.BookDaoImpl"/>

<bean id="myadvice" class="com.njust.learning.spring.aop.MyAdvice"/>

<bean id="rmpAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
    <property name="patterns" value=".*add"/>
    <property name="advice" ref="myadvice"/>
</bean>

<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
    <property name="beanNames" value="*Dao"/>
    <property name="interceptorNames" value="rmpAdvisor"/>
</bean>

Using DefaultAdvisorAutoProxyCreator to create proxies based on advisors:

<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/>

Pure POJO aspect configuration (XML)

public interface IBookDao {
    public int add();
    public int delete();
}

public class BookDaoImpl implements IBookDao {
    public int add() {
        int a = 1/0;
        System.out.println("正在添加图书...");
        return 0;
    }
    public int delete() {
        System.out.println("正在删除图书...");
        return 0;
    }
}

public class PojoAdvice {
    public void before() {
        System.out.println("前置通知");
    }
    public void after(Object returnval) {
        System.out.println("后置通知,处理后的结果为:" + returnval);
    }
    public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("环绕前置增强...");
        Object o = proceedingJoinPoint.proceed();
        System.out.println("环绕后置增强...");
        return o;
    }
    public void afterThrowing(Throwable e) {
        System.out.println("异常通知:" + e.getMessage());
    }
}
<bean id="bookDao" class="com.njust.learning.spring.service.BookDaoImpl"/>

<bean id="pojoAdvice" class="com.njust.learning.spring.pojoaop.PojoAdvice"/>

<aop:config>
    <aop:pointcut id="p" expression="execution (* *.add(..))"/>
    <aop:aspect ref="pojoAdvice">
        <aop:before method="before" pointcut-ref="p"/>
        <aop:after-returning method="after" pointcut-ref="p" returning="returnval"/>
        <aop:around method="around" pointcut-ref="p"/>
        <aop:after-throwing method="afterThrowing" pointcut-ref="p" throwing="e"/>
    </aop:aspect>
</aop:config>
Spring AOP’s underlying technology still relies on JDK dynamic proxies and CGLIB.

Spring’s AOP namespace allows converting pure POJOs into aspects, but this XML‑based approach cannot use annotations. By leveraging AspectJ’s annotation support, Spring enables annotation‑driven AOP, eliminating the need for XML configuration, though it requires the additional aspectjweaver.jar library.

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.

JavaaopBackend Developmentaspectj
Java High-Performance Architecture
Written by

Java High-Performance Architecture

Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.

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.