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(){};
        
    }
  • 相关阅读:
    分布式01-Dubbo基础背景
    项目总结17-使用layui table分页表格
    项目总结16-创建验证码图片
    Springboot学习07-数据源Druid
    Springboot学习06-Spring AOP封装接口自定义校验
    Springboot学习05-自定义错误页面完整分析
    Springboot学习04-默认错误页面加载机制源码分析
    Springboot学习03-SpringMVC自动配置
    项目总结15:JavaScript模拟表单提交(实现window.location.href-POST提交数据效果)
    Springboot学习02-webjars和静态资源映射规则
  • 原文地址:https://www.cnblogs.com/flytogalaxy/p/7404918.html
Copyright © 2011-2022 走看看