切面优先级 Spring Aop

本文介绍如何在Spring AOP中配置多个切面的执行顺序。通过设置order属性来确保缓存、数据源和事务等切面按需正确执行。缓存切面在最前执行,接着是数据源切面,最后是事务切面。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、当代码中有多个业务切面使用到同一个切入点的时候,这时候就需要确定切面的优先级了

<!-- 缓存>数据源>事务 -->
<aop:config>
	<!-- 切入点(service的所有方法) -->
	<aop:pointcut id="txPointcut" expression="execution(* com.xxxxx.service.*.*(..))" />
	<!-- 事务切面-->
	<aop:advisor pointcut-ref="txPointcut" advice-ref="txAdvice" order="2"/>
	<!-- druid spring切面监控 -->
	<aop:advisor advice-ref="druid-stat-interceptor" pointcut-ref="druid-stat-pointcut" order="-2"/>
	<!-- 数据源切面 -->
	<aop:aspect ref="dataSourceAspect" order="1">
	    <aop:before method="before" pointcut-ref="txPointcut" />
	</aop:aspect>
	<!-- 缓存切面 -->
	<aop:aspect ref="cacheAspect" order="-1">
		<aop:around method="aroundMethod" pointcut-ref="txPointcut"/>
	</aop:aspect>
</aop:config>
2、aop:advisor就是一个特殊的切面

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值