一、前置通知

 1 import org.aspectj.lang.JoinPoint;
 2 import org.aspectj.lang.ProceedingJoinPoint;
 3 import org.aspectj.lang.annotation.After;
 4 import org.aspectj.lang.annotation.AfterReturning;
 5 import org.aspectj.lang.annotation.AfterThrowing;
 6 import org.aspectj.lang.annotation.Around;
 7 import org.aspectj.lang.annotation.Aspect;
 8 import org.aspectj.lang.annotation.Before;
 9 import org.aspectj.lang.annotation.Pointcut;
10 
11 @Aspect  //表示当前类为切面
12 public class MyAspect {
13    @Before("execution(* *..ISomeService.doFirst(..))")
14    public void  myBefore(){
15        System.out.println("执行前置通知方法");
16    }
17    
18    @Before("execution(* *..ISomeService.doFirst(..))")
19    public void   myBefore(JoinPoint jp){
20        System.out.println("执行前置通知方法 jp="+jp);
21    }
22    
23    @AfterReturning("execution(* *..ISomeService.doSecond(..))")
24    public void myAfterReturning(){
25        System.out.println("执行后置通知方法");
26        
27    }
28    
29    @AfterReturning(value="execution(* *..ISomeService.doSecond(..))",returning="result")
30    public void myAfterReturning(Object result){
31        System.out.println("执行后置通知方法 result="+result);
32        
33    }
34    
35    @Around("execution(* *..ISomeService.doSecond(..))")
36    public Object myAround(ProceedingJoinPoint pjp) throws Throwable{
37        System.out.println("执行环绕通知方法,目标方法执行之前");
38        //执行目标方法
39        Object result = pjp.proceed();
40        System.out.println("执行环绕通知方法,目标方法执行之后");
41        if(result !=null){
42             result=((String)result).toUpperCase();
43        }
44        return result;  
45    }
46    
47    @AfterThrowing(value="execution(* *..ISomeService.doThird(..))",throwing="ex")
48    public void myAfterThrowing(Exception ex){
49       System.out.println("执行异常通知方法ex="+ex.getMessage());  
50    }
51    
52    @After("doThirdPointcut()")
53    public void myAfter(){
54       System.out.println("执行最终通知方法");  
55    }
56    //定义了一个切入点,叫doThirdPointcut()
57    @Pointcut("execution(* *..ISomeService.doThird(..))")
58    public void doThirdPointcut(){}
59 }
MyAspect

相关文章:

  • 2021-12-01
  • 2022-01-16
  • 2021-09-26
  • 2022-12-23
  • 2022-12-23
  • 2021-05-11
猜你喜欢
  • 2022-01-14
  • 2021-11-27
  • 2022-01-09
  • 2021-11-29
  • 2021-09-13
相关资源
相似解决方案