个人笔记,附带个人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>