博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
springboot中的5种通知小例子
阅读量:6904 次
发布时间:2019-06-27

本文共 6344 字,大约阅读时间需要 21 分钟。

springboot中的5种通知的小例子

1.环境搭建

  • pom中导入
    org.springframework.boot
    spring-boot-starter-aop
application.properties文件中配置    #Spring AOP    spring.aop.auto=true    spring.aop.proxy-target-class=true
类上面增加@Aspect标注

2.切入点表达式

/**     * 定义一个方法, 用于声明切入点表达式. 一般地, 该方法中再不需要添入其他的代码.     * 使用 @Pointcut 来声明切入点表达式.     * 后面的其他通知直接使用方法名来引用当前的切入点表达式.     * (..)表示任意参数     */    @Pointcut("execution(public int com.jztey.omronhealth.service.ArithmeticCalculator.*(..))")    public void declareJointPointExpression() {    }

3.前置通知

//声明该方法执行之前执行,前置通知    //直接导入切面点,上面的    @Before("declareJointPointExpression()")    public void beforeMethod(JoinPoint joinPoint) {        String methodName = joinPoint.getSignature().getName();        Object[] args = joinPoint.getArgs();        System.out.println("这是切面开始打印出来的--->The method " + methodName + " begins with " + Arrays.asList(args));    }

4.后置通知

//后置通知:在目标方法执行后(无论是否发生异常),执行通知    //在后置通知中还不能访问目标方法的执行的结果,不是在执行方法后调用的    /**     * 这是切面开始打印出来的--->The method add begins with [3, 5]     * 这是切面结束打印出来的--->The method add ends     * 和--->8     */    @After("declareJointPointExpression()")    public void afterMethod(JoinPoint joinPoint) {        String methodName = joinPoint.getSignature().getName();        System.out.println("这是切面结束打印出来的--->The method " + methodName + " ends");    }

5.带有返回值通知

/**     * 带有返回值的切面     * 在方法法正常结束受执行的代码     * 返回通知是可以访问到方法的返回值的!     * 可以使用returning = "result"进行获取后得到     */    @AfterReturning(value = "declareJointPointExpression()",            returning = "result")    public void afterReturning(JoinPoint joinPoint, Object result) {        String methodName = joinPoint.getSignature().getName();        System.out.println("The method " + methodName + " ends with " + result);    }

6.异常通知

/**     * 异常处理切面     * 在目标方法出现异常时会执行的代码.     * 可以访问到异常对象; 且可以指定在出现特定异常时在执行通知代码     */    @AfterThrowing(value = "declareJointPointExpression()",            throwing = "e")    public void afterThrowing(JoinPoint joinPoint, Exception e) {        String methodName = joinPoint.getSignature().getName();        System.out.println("The method " + methodName + " occurs excetion:" + e);    }

7.环绕通知

/** * 环绕切面,一般用的不是很多,类似于动态代理,可以包含前面4种的任意个 * 环绕通知需要携带 ProceedingJoinPoint 类型的参数. * 环绕通知类似于动态代理的全过程: ProceedingJoinPoint 类型的参数可以决定是否执行目标方法. * 且环绕通知必须有返回值, 返回值即为目标方法的返回值 */    /*    @Around("execution(public int com.atguigu.spring.aop.ArithmeticCalculator.*(..))")    public Object aroundMethod(ProceedingJoinPoint pjd){        Object result = null;        String methodName = pjd.getSignature().getName();        try {            //前置通知            System.out.println("The method " + methodName + " begins with " + Arrays.asList(pjd.getArgs()));            //执行目标方法            result = pjd.proceed();            //返回通知            System.out.println("The method " + methodName + " ends with " + result);        } catch (Throwable e) {            //异常通知            System.out.println("The method " + methodName + " occurs exception:" + e);            throw new RuntimeException(e);        }        //后置通知        System.out.println("The method " + methodName + " ends");        return result;    }    */

优先级

/** * 可以使用 @Order 注解指定切面的优先级, 值越小优先级越高 */@Order(2)

完整的切面例子

//通过添加 @Aspect 注解声明一个 bean 是一个切面!/** * 可以使用 @Order 注解指定切面的优先级, 值越小优先级越高 */@Order(2)@Aspect@Component@Slf4jpublic class LoggingAspect {    /**     * 定义一个方法, 用于声明切入点表达式. 一般地, 该方法中再不需要添入其他的代码.     * 使用 @Pointcut 来声明切入点表达式.     * 后面的其他通知直接使用方法名来引用当前的切入点表达式.     * (..)表示任意参数     */    @Pointcut("execution(public int com.jztey.omronhealth.service.ArithmeticCalculator.*(..))")    public void declareJointPointExpression() {    }    //声明该方法执行之前执行,前置通知    //直接导入切面点,上面的    @Before("declareJointPointExpression()")    public void beforeMethod(JoinPoint joinPoint) {        String methodName = joinPoint.getSignature().getName();        Object[] args = joinPoint.getArgs();        System.out.println("这是切面开始打印出来的--->The method " + methodName + " begins with " + Arrays.asList(args));    }    //后置通知:在目标方法执行后(无论是否发生异常),执行通知    //在后置通知中还不能访问目标方法的执行的结果,不是在执行方法后调用的    /**     * 这是切面开始打印出来的--->The method add begins with [3, 5]     * 这是切面结束打印出来的--->The method add ends     * 和--->8     */    @After("declareJointPointExpression()")    public void afterMethod(JoinPoint joinPoint) {        String methodName = joinPoint.getSignature().getName();        System.out.println("这是切面结束打印出来的--->The method " + methodName + " ends");    }    /**     * 带有返回值的切面     * 在方法法正常结束受执行的代码     * 返回通知是可以访问到方法的返回值的!     * 可以使用returning = "result"进行获取后得到     */    @AfterReturning(value = "declareJointPointExpression()",            returning = "result")    public void afterReturning(JoinPoint joinPoint, Object result) {        String methodName = joinPoint.getSignature().getName();        System.out.println("The method " + methodName + " ends with " + result);    }    /**     * 异常处理切面     * 在目标方法出现异常时会执行的代码.     * 可以访问到异常对象; 且可以指定在出现特定异常时在执行通知代码     */    @AfterThrowing(value = "declareJointPointExpression()",            throwing = "e")    public void afterThrowing(JoinPoint joinPoint, Exception e) {        String methodName = joinPoint.getSignature().getName();        System.out.println("The method " + methodName + " occurs excetion:" + e);    }/** * 环绕切面,一般用的不是很多,类似于动态代理,可以包含前面4种的任意个 * 环绕通知需要携带 ProceedingJoinPoint 类型的参数. * 环绕通知类似于动态代理的全过程: ProceedingJoinPoint 类型的参数可以决定是否执行目标方法. * 且环绕通知必须有返回值, 返回值即为目标方法的返回值 */    /*    @Around("execution(public int com.atguigu.spring.aop.ArithmeticCalculator.*(..))")    public Object aroundMethod(ProceedingJoinPoint pjd){        Object result = null;        String methodName = pjd.getSignature().getName();        try {            //前置通知            System.out.println("The method " + methodName + " begins with " + Arrays.asList(pjd.getArgs()));            //执行目标方法            result = pjd.proceed();            //返回通知            System.out.println("The method " + methodName + " ends with " + result);        } catch (Throwable e) {            //异常通知            System.out.println("The method " + methodName + " occurs exception:" + e);            throw new RuntimeException(e);        }        //后置通知        System.out.println("The method " + methodName + " ends");        return result;    }    */}

转载于:https://blog.51cto.com/yushiwh/2287386

你可能感兴趣的文章
前端:单页面应用和多页面应用
查看>>
使用iview的Table组件实现合并列demo
查看>>
【LeetCode】26. 删除排序数组中的重复项
查看>>
Spring Boot 参考指南(使用JTA分布式事务)
查看>>
Express 项目结构最佳实践(下)
查看>>
前端面试之JavaScript(总结)
查看>>
字符串匹配模式问题
查看>>
【勘误】第三章基本变量
查看>>
iOS天气动画、高仿QQ菜单、放京东APP、高仿微信、推送消息等源码
查看>>
ABAP和Java的destination和JNDI
查看>>
vue-router 3.0版本中 router.push 不能刷新页面的问题
查看>>
C++入门教程(3):语句和缩进
查看>>
菜鸟入门【ASP.NET Core】11:应用Jwtbearer Authentication、生成jwt token
查看>>
RedMonk 2018年6月编程语言排行:Objective-C 升至第九
查看>>
2016全域大数据应用论坛11位嘉宾核心观点
查看>>
.NetCore获取Json和Xml格式的配置信息
查看>>
window下解压.tar文件的软件
查看>>
整数和浮点数的表示方法
查看>>
MNIST神经网络的训练
查看>>
先制定一个能达到的小目标,比方说先让无人车合法上路
查看>>