6 - 基于注解形式的AOP

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("[注解]最终通知");
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值