2 基于XML实现AOP
XML配置实现AOP:通过XML配置AOP是Spring框架专有的,但由于AspectJ得到越来越多的AOP框架支持,所以通过注解实现AOP将会有更多重用的机会。通过XML配置实现AOP代码如下:
CalculatorAspect类:
package com.jd.calculator;
import org.aspectj.lang.JoinPoint;
public class CalculatorAspect {
//前置增强
public void before(JoinPoint joinPoint) {
Object target=joinPoint.getTarget();
String methodName = joinPoint.getSignature().getName();
Object[] params = joinPoint.getArgs();
System.out.println(target.getClass().getName()+":The "+methodName+" method begins.");
System.out.println(target.getClass().getName()+":Parameters of the "+methodName+" method: ["+params[0]+","+params[1]+"]");
}
//返回增强
public void afterReturning(JoinPoint