Xml方式配置执行顺序
<aop:config>
<aop:pointcut id="pc"
expression="execution(*
com.company.spring.service..*.*(..))"/>
<aop:aspect ref="loggingAspect" order="1">
<aop:around method="aroundMethod" pointcut-ref="pc"/>
</aop:aspect>
<aop:aspect ref="txManager" order="2">
<aop:around method="aroundMethod" pointcut-ref="pc"/>
</aop:aspect>
</aop:config>
注解方式顺序配置需要借助@Order注解
@Order(1)
@Aspect
@Component
public class TxManager {
@Pointcut("execution(* com.company.spring.service..*.(..))")
public void pointCut() {}
@Around("pointCut()")
public Object around(ProceedingJoinPoint joinPoint)
throws Throwable{
System.out.println("事务开始");
Object result = joinPoint.proceed();
System.out.println("事务结束");
return result;
}
}
注解方式顺序配置
@Order(2)
@Aspect
@Component
public class LoggingAspect {
@Pointcut("execution(* com.company.spring.service..*.(..))")
public void pointCut() {}
@Before("pointCut()")
public void beforeMethod() {
System.out.println("beforeMethod");
}
@Before("pointCut()")
public void afterMethod() {
System.out.println("afterMethod");
}
}