jar包
aopalliance.jar
aspectjweaver.jar
配置
将业务类、通知纳入SpringIOC容器里
1 - 开启注解对AOP的支持
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
编写
1 - 类注解
@Aspect:声明该类是个通知
2 - 方法注解
前置通知
@Before("{expression}")
后置通知
@AfterReturning("{expression}")
package com.qsd.aop;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Component("annoBefore")
@Aspect
public class BeforeAnnotation {
@Before("execution(public int com.qsd.util.DBUtil.deleteStudent(int))")//属性:定义切点
public void MyLog(JoinPoint js) {
System.out.println("[注解]前置通知:即将执行deleteStudent()方法");
}
@AfterReturning("execution(public int com.qsd.util.DBUtil.deleteStudent(int))")//属性:定义切点
public void After(JoinPoint js,Object returningValue){
}
}
获取目标方法的参数
JoinPoint
//目标对象
js.getTarget();
//方法名
js.getSignature().getName()
//参数列表
js.getArgs()
//返回值
returningValue
获取返回值
@AfterReturning(pointcut = "execution(public int com.qsd.util.DBUtil.deleteStudent(int))",returning = "returningValue")
public void AfterLog(JoinPoint js,Object returningValue) {
System.out.println("[注解]后置通知:目标方法:"+js.getSignature().getName()+"返回值:"+returningValue);
}
参考
package com.qsd.aop;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Component("annoBefore")
@Aspect
public class BeforeAnnotation {
//前置通知
@Before("execution(public int com.qsd.util.DBUtil.deleteStudent(int))")//属性:定义切点
public void MyBefor(JoinPoint jp) {
System.out.println("[注解]前置通知:即将执行deleteStudent()方法");
}
//后置通知
@AfterReturning(pointcut = "execution(public int com.qsd.util.DBUtil.deleteStudent(int))",returning = "returningValue")
public void MyAfter(JoinPoint jp,Object returningValue) {
System.out.println("[注解]后置通知:已经执行完deleteStudent()方法。"+"返回值:"+returningValue);
}
//环绕通知
@Around(value = "execution(public int com.qsd.util.DBUtil.deleteStudent(int))")
public Object MyAround(ProceedingJoinPoint pj) {
Object result = null;
//方法执行之前
System.out.println("[注解][环绕]前置通知");
try {
result = pj.proceed();
//方法执行之后
System.out.println("[注解][环绕]后置通知");
}catch (Throwable e) {
//发生异常时
System.out.println("[注解][环绕]异常通知:执行deleteStudent()方法出现异常");
}finally {
//最终通知
System.out.println("[注解][环绕]最终通知");
}
return result;
}
//异常通知:如果只捕获特定异常的通知,可以使用第二个参数
@AfterThrowing(pointcut = "execution(public int com.qsd.util.DBUtil.deleteStudent(int))",throwing = "e")
public void MyException(JoinPoint jp,NullPointerException e) {//此异常通知只会捕获NullPointerException异常
System.out.println("[注解]异常通知:执行deleteStudent()方法出现异常"+"异常:"+e.getMessage());
}
//最终通知
@After("execution(public int com.qsd.util.DBUtil.deleteStudent(int))")
public void MyFinally() {
System.out.println("[注解]最终通知");
}
}