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支持 | 好 | 一般 |
| 推荐度 | ⭐⭐⭐⭐⭐ | ⭐⭐⭐ |
要点总结
- Spring AOP支持注解和XML两种配置方式
- 注解方式需@EnableAspectJAutoProxy开启支持
- @Aspect定义切面,@Pointcut定义切入点
- XML方式通过aop:config配置切面
- 注解方式更简洁,推荐使用
📝 发现内容有误?点击此处直接编辑