AOP 切面类实现计算器功能增强 - 显示运算数、除零错误处理
public aspect ValidationAspect {
pointcut calculation(): execution(* Calculator.*(..));
pointcut division(): execution(* Calculator.div(..));
before(): calculation() {
System.out.println('运算数为:' + thisJoinPoint.getArgs()[0] + ' 和 ' + thisJoinPoint.getArgs()[1]);
}
before(): division() {
if(thisJoinPoint.getArgs()[1] == 0) {
System.out.println('当前除数为0,请修改后重新计算');
throw new IllegalArgumentException('除数不能为0');
}
}
}
原文地址: https://www.cveoy.top/t/topic/ozmf 著作权归作者所有。请勿转载和采集!