Spring 嵌套AOP 的顺序问题 指定Aspect优先级
Spring3 M2
另外所需要的jar
aopalliance-1.0.jar
aspectjrt-1.6.8.jar
aspectjweaver-1.6.8.jar
cglib-nodep-2.2.3.jar
package com.epkj.test; import org.aspectj.lang.ProceedingJoinPoint; public class Aop1 { public void log(ProceedingJoinPoint point) throws Throwable { System.out.println("Aop1.log1()-begion"); point.proceed(); System.out.println("Aop1.log1()-end"); } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.2.xsd"> <context:component-scan base-package="com.epkj.*" /> <aop:config> <aop:aspect id="asoect1" ref="aop1" order="2"> <aop:pointcut expression="execution(* com.epkj.test.*.*(..))" id="allMethod"/> <aop:around method="log" pointcut-ref="allMethod"/> </aop:aspect> </aop:config> <aop:config> <aop:aspect id="asoect2" ref="aop2" order="1"> <aop:pointcut expression="execution(* com.epkj.test.*.*(..))" id="allMethod"/> <aop:around method="log" pointcut-ref="allMethod"/> </aop:aspect> </aop:config> <bean id="aop1" class="com.epkj.test.Aop1" /> <bean id="aop2" class="com.epkj.test.Aop2" /> </beans>
<aop:aspect id="asoect2" ref="aop2" order="1">
order属性用来指定AOP的顺序
还有其他几种方式
package com.codeproject.jackie.springrecipesnote.springaop; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.core.Ordered; /** * @author jackie * */ @Aspect public class CalculatorValidationAspect implements Ordered{ private Log log = LogFactory.getLog(this.getClass()); @Before("execution(* *.*(double, double))") public void validateBefore(JoinPoint joinPoint) { for (Object arg : joinPoint.getArgs()) { log.info("validate begins..."); validate((Double) arg); } } private void validate(Double arg) { if (arg < 0) { throw new IllegalArgumentException("Positive numbers only"); } } @Override public int getOrder() { return 0; } }
package com.codeproject.jackie.springrecipesnote.springaop; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.core.annotation.Order; /** * @author jackie * */ @Aspect @Order(0) public class CalculatorValidationAspect { private Log log = LogFactory.getLog(this.getClass()); @Before("execution(* *.*(double, double))") public void validateBefore(JoinPoint joinPoint) { for (Object arg : joinPoint.getArgs()) { log.info("validate begins..."); validate((Double) arg); } } private void validate(Double arg) { if (arg < 0) { throw new IllegalArgumentException("Positive numbers only"); } } }