关于Spring的AOP的通知类型(在AspectJ)的情况下使用

package com.layne.spring.aspect.aspects;

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.aspectj.lang.annotation.Pointcut;

@Aspect //表示当前POJO类为切面
public class MyAspect {
    
    //定义前置通知方法
    @Before("execution(* *..ISomeService.doSome(..))")
    public void myBefore(){
        System.out.println("执行前置通知方法myBefore");
    }
    //定义前置通知方法
    @Before(value = "execution(* *..ISomeService.doSome(..))")
    public void myBefore(JoinPoint jp){  //所有通知类型下,都有这个参数
        System.out.println("执行前置通知方法   Jp="+jp);
    }
    //定义前置通知方法
    @AfterReturning("execution(* *..ISomeService.doSecond(..))")
    public void afterReturning(){
        System.out.println("执行后置通知方法  ``````");
    }
    //定义后置通知方法
    @AfterReturning(value = "execution(* *..ISomeService.doSecond(..))",returning="result")
   public void afterReturning(Object result){
        System.out.println("执行后置通知方法  result="+result);
    }
    //环绕通知方法
    @Around("execution(* *..ISomeService.doThird(..))")
   public Object MyAround(ProceedingJoinPoint pjp) throws Throwable{
        System.out.println("执行目标方法之前执行···000000");
        Object proceed = pjp.proceed();
        String upperCase = ((String)proceed).toUpperCase();
        System.out.println("执行目标方法之后执行····00000");
        return upperCase;
        
    }
    //异常通知
    @AfterThrowing(value="execution(* *..ISomeService.doSome(..))",throwing="ex")
    public void myThrows(Exception ex){
        System.out.println("执行异常通知······ex="+ex.getMessage());
    }
    
    //最终通知
    @After("doSomePointCut()")
    public void myAfter(){
        System.out.println("执行最终通知+++++++00999999999000+++++++");
    }
    //定义通知的切点
    @Pointcut(value="execution(* *..ISomeService.doSome(..))")
    private void doSomePointCut(){};
    
}

 

posted @ 2017-08-21 16:03  夜西门吹雪孤城花满楼  阅读(371)  评论(0编辑  收藏  举报