1. 加入 jar 包
2. 在 Spring 的配置文件中加入 aop 的命名空间。
3. 基于注解的方式来使用 AOP
3.1 在配置文件中配置自动扫描的包:
<context:component-scan base-package="bean"></context:component-scan>
3.2 加入使 AspjectJ 注解起作用的配置:
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
4. 编写切面类:
4.1 一个一般的 Java 类
4.2 在其中添加要额外实现的功能.
5. 配置切面
5.1 切面必须是 IOC 中的 bean: 实际添加了 @Component 注解
5.2 声明是一个切面: 添加 @Aspect
5.3 声明通知: 即额外加入功能对应的方法.
eg:
前置通知:
@Before("execution(public int com.atguigu.spring.aop.ArithmeticCalculator.*(int, int))")
@Before 表示在目标方法执行之前执行 @Before 标记的方法的方法体.
@Before 里面的是切入点表达式:
6. 在通知中访问连接细节: 可以在通知方法中添加 JoinPoint 类型的参数,从中可以访问到方法的签名和方法的参数.