Spring利用注解实现5个通知

个人笔记,附带个人demo

<span style="font-size:24px;">package Summary;

public interface Computer {
	int add(int i,int j);
	int div(int i,int j);
}</span>

<span style="font-size:24px;">package Summary;

import org.springframework.stereotype.Component;


@Component
public class Pc implements Computer{

	public int add(int i, int j) {
		return i+j;
	}

	public int div(int i, int j) {
		return i/j;
	}

}</span>

<span style="font-size:24px;">package Summary;

import java.util.Arrays;

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;


/*
 * Spring的5个通知
 * 前置通知
 * 后置通知
 * 返回通知
 * 异常通知
 * 环绕通知
 * 原理是动态代理,method.invoke的前置后置返回和异常捕捉
 * 而环绕通知是模拟整个过程
 */
@Order(1)
@Component
@Aspect
public class TestAspect {

<span style="white-space:pre">	/*
<span style="white-space:pre">	</span> * Order注解 用来指明切面优先级,数字越小优先级越大
<span style="white-space:pre">	</span> */
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>/*
<span style="white-space:pre">	</span> * 给注解加入属性时,可以重用代码
<span style="white-space:pre">	</span> */
<span style="white-space:pre">	</span>@Pointcut("execution(* Summary.Pc.*(..))")
<span style="white-space:pre">	</span>public void re(){}</span>
	
	@Before("re()")
	/*
	 * 前置通知,连接点参数JoinPoint (可选)
	 */
	public void before(JoinPoint joinPoint){
		String methodName = joinPoint.getSignature().getName();
		System.out.println("the method name is "+methodName+" and begin with" +Arrays.asList(joinPoint.getArgs()));
	}
	
	
	@AfterReturning(value="execution(* Summary.Pc.*(..))",returning="result")
	/*
	 * 返回通知,连接点参数JoinPoint (可选),返回通知可以得到返回值result
	 */
	public void afterReturning(JoinPoint joinPoint,Object result){
		String methodName = joinPoint.getSignature().getName();
		System.out.println("the method name is "+methodName+" and result is " + result);
	}
	
	/*
	 * 异常通知 可以得到异常参数
	 */
	
	@AfterThrowing(value="execution(* Summary.Pc.*(..))",throwing="e")
	public void afterThrowing(JoinPoint joinPoint,Exception e){
		String methodName = joinPoint.getSignature().getName();
		System.out.println("the method name is "+methodName+" and occur with exception: " + e);
	}
	
	

	@After("execution(* Summary.Pc.*(..))")
	/*
	 * 后置通知,连接点参数JoinPoint (可选),后置通知无法得到返回值result
	 */
	public void after(JoinPoint joinPoint){
		String methodName = joinPoint.getSignature().getName();
		System.out.println("the method name is "+methodName+" and end with" +Arrays.asList(joinPoint.getArgs()));
	}
	
	/*
	 * 环绕通知:ProceedingJoinPoint指各个参数点
	*/
	@Around("execution(* Summary.Pc.*(..))")
	public Object around(ProceedingJoinPoint joinPoint){
		Object result = null;
		System.out.println("before");
		try {
			result = joinPoint.proceed();
			System.out.println("returning");
		} catch (Throwable e) {
			System.out.println("exception");
		}
		System.out.println("after");
		return result;
		
	}
	 
}
</span>


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值