全部学科
Python全栈
python
NodeJS全栈
nodejs
小程序首页
📅 2026-05-18 10 分钟 ✍️ juanwangdev

AOP实现方式

Spring AOP提供两种实现方式:注解方式和XML配置方式。注解方式更简洁,是主流开发方式。

注解方式实现

1. 添加依赖

XML
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

2. 开启AOP支持

Java
@Configuration
@EnableAspectJAutoProxy
public class AopConfig {
}

3. 定义切面

Java
@Aspect
@Component
public class LoggingAspect {

    // 定义切入点
    @Pointcut("execution(* com.example.service.*.*(..))")
    public void serviceLayer() {}

    // 前置通知
    @Before("serviceLayer()")
    public void beforeMethod(JoinPoint joinPoint) {
        System.out.println("执行方法: " + joinPoint.getSignature().getName());
    }

    // 环绕通知
    @Around("serviceLayer()")
    public Object aroundMethod(ProceedingJoinPoint joinPoint) throws Throwable {
        long start = System.currentTimeMillis();
        Object result = joinPoint.proceed();
        System.out.println("耗时: " + (System.currentTimeMillis() - start) + "ms");
        return result;
    }
}

4. 目标类

Java
@Service
public class UserService {

    public void save(String name) {
        System.out.println("保存用户: " + name);
    }

    public String find(Long id) {
        return "用户" + id;
    }
}

XML配置方式

1. 配置文件

XML
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop">

    <!-- 开启AOP支持 -->
    <aop:aspectj-autoproxy/>

    <!-- 目标Bean -->
    <bean id="userService" class="com.example.service.UserService"/>

    <!-- 切面Bean -->
    <bean id="loggingAspect" class="com.example.aspect.LoggingAspect"/>

    <!-- AOP配置 -->
    <aop:config>
        <!-- 切入点 -->
        <aop:pointcut id="serviceLayer"
                      expression="execution(* com.example.service.*.*(..))"/>

        <!-- 切面 -->
        <aop:aspect ref="loggingAspect">
            <!-- 前置通知 -->
            <aop:before pointcut-ref="serviceLayer" method="beforeMethod"/>
            <!-- 环绕通知 -->
            <aop:around pointcut-ref="serviceLayer" method="aroundMethod"/>
        </aop:aspect>
    </aop:config>
</beans>

2. 切面类(无需注解)

Java
public class LoggingAspect {

    public void beforeMethod(JoinPoint joinPoint) {
        System.out.println("执行方法: " + joinPoint.getSignature().getName());
    }

    public Object aroundMethod(ProceedingJoinPoint joinPoint) throws Throwable {
        long start = System.currentTimeMillis();
        Object result = joinPoint.proceed();
        System.out.println("耗时: " + (System.currentTimeMillis() - start) + "ms");
        return result;
    }
}

Schema-based AOP

纯XML配置,不使用@AspectJ注解。

XML
<aop:config>
    <aop:pointcut id="serviceMethod"
                  expression="execution(* com.example.service.*.*(..))"/>

    <aop:aspect ref="loggingAspect">
        <aop:before pointcut-ref="serviceMethod" method="logBefore"/>
        <aop:after-returning pointcut-ref="serviceMethod"
                             method="logAfterReturning"
                             returning="result"/>
        <aop:after-throwing pointcut-ref="serviceMethod"
                            method="logException"
                            throwing="ex"/>
        <aop:around pointcut-ref="serviceMethod" method="logAround"/>
    </aop:aspect>
</aop:config>

两种方式对比

特性注解方式XML方式
代码量
可读性配置分散
维护性方便需修改XML
IDE支持一般
推荐度⭐⭐⭐⭐⭐⭐⭐⭐

要点总结

  1. Spring AOP支持注解和XML两种配置方式
  2. 注解方式需@EnableAspectJAutoProxy开启支持
  3. @Aspect定义切面,@Pointcut定义切入点
  4. XML方式通过aop:config配置切面
  5. 注解方式更简洁,推荐使用

📝 发现内容有误?点击此处直接编辑

← 上一篇 AOP切入点表达式
下一篇 → AOP应用场景
想查看更多题目和详细解析?
小程序提供完整的题库、模拟考试和详细解析
马上就来

长按或扫描二维码,立即体验

扫码体验小程序
马上就来
使用微信扫描二维码
立即体验完整题库