匹配优先级Spring攻略学习笔记(3.04)------指定Aspect优先级
文章结束给大家来个程序员笑话:[M]
一、知识点
当雷同连接点上应用了多个aspect时,aspect的优先级是不明确的,除非显式地指定它们的优先级。
aspect的优先级可以通过现实Ordered口接或者用使@Order注解现实。
二、代码示例
(1)现实Ordered口接
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.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.Ordered;
/**
* @author jackie
*
*/
@Aspect
public class CalculatorLoggingAspect implements Ordered{
private Log log = LogFactory.getLog(this.getClass());
/**
* 切入点表达式匹配ArithmeticCalculator口接的add()方法的执行。
* 表达式前导的星号匹配任何修饰符(public、protected和private)和任何回返类型。
* 参数列表中的两个点匹配任何量数的参数。
*/
@Before("execution(* ArithmeticCalculator.add(..))")
public void logBefore() {
log.info("The method add() begins");
}
@Override
public int getOrder() {
return 1;
}
}
只要在Bean配置文件中声明这个aspect的一个Bean例实,以可就在Spring中注册这个aspect:
<bean class="com.codeproject.jackie.springrecipesnote.springaop.CalculatorLoggingAspect" /> <bean class="com.codeproject.jackie.springrecipesnote.springaop.CalculatorValidationAspect" />
注意:aspect的优先级不取决于Bean声明的次序。getOrder()方法回返的值越低就代表越高的优先级。
(2)@Order注解
另一种指定优先级的方法是通过@Order注解。次序值在注解值中涌现。
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");
}
}
}
package com.codeproject.jackie.springrecipesnote.springaop;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;
/**
* @author jackie
*
*/
@Aspect
@Order(1)
public class CalculatorLoggingAspect{
private Log log = LogFactory.getLog(this.getClass());
/**
* 切入点表达式匹配ArithmeticCalculator口接的add()方法的执行。
* 表达式前导的星号匹配任何修饰符(public、protected和private)和任何回返类型。
* 参数列表中的两个点匹配任何量数的参数。
*/
@Before("execution(* ArithmeticCalculator.add(..))")
public void logBefore() {
log.info("The method add() begins");
}
}
文章结束给大家分享下程序员的一些笑话语录:
程序员的愿望
有一天一个程序员见到了上帝.上帝: 小伙子,我可以满足你一个愿望.程序员: 我希望中国国家队能再次打进世界杯.
上帝: 这个啊!这个不好办啊,你还说下一个吧!
程序员: 那好!我的下一个愿望是每天都能休息6个小时以上.
上帝: 还是让中国国家打进世界杯.

浙公网安备 33010602011771号