zoukankan      html  css  js  c++  java
  • 关于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(){};
        
    }
  • 相关阅读:
    5G和物联网:面临各种安全挑战的新兴技术
    嵌入式Linux系统的几大组件!
    物联网应用开发如何平衡用户体验与隐私安全?
    我们需要什么数据架构?
    2020.7.30
    2020.7.29
    2020.7.28
    2020.7.27
    2020.7.26 + 周报(3)
    2020.7.25
  • 原文地址:https://www.cnblogs.com/flytogalaxy/p/7404918.html
Copyright © 2011-2022 走看看