执行 -->
<aop:before method="beginTransaction" pointcut-ref="transactionPointcut" />
<!-- 【后置通知】 -->
<aop:after method="commit" pointcut-ref="transactionPointcut"/>
<!-- 【返回后通知】 -->
<aop:after-returning method="afterReturing" pointcut-ref="transactionPointcut"/>
<!-- 异常通知 -->
<aop:after-throwing method="afterThrowing" pointcut-ref="transactionPointcut"/>
</aop:aspect>
</aop:config>
</beans>
切入点表达式不在这里介绍。参考 Spring AOP 切入点表达式
代码的测试结果如下:
到这里,我们已经全部介绍完Spring AOP。回到开篇的问题,我们拿它做什么?
- Spring声明式事务管理配置:请参考博主的另一篇文章:分布式系统架构实战 demo:SSM+Dubbo
- Controller层的参数校验:参考 Spring AOP拦截Controller做参数校验
- 使用 Spring AOP 实现 MySQL 数据库读写分离案例分析
- 在执行方法前,判断是否具有权限
- 对部分函数的调用进行日志记录:监控部分重要函数,若抛出指定的异常,可以以短信或邮件方式通知相关人员。
- 信息过滤,页面转发等等功能
博主一个人的力量有限,只能列举这么多,欢迎评论区对文章做补充。
Spring AOP还能做什么,实现什么魔幻功能,就在于我们每一个平凡而又睿智的程序猿!