博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
19Spring返回通知&异常通知&环绕通知
阅读量:4318 次
发布时间:2019-06-06

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

在前置通知和后置通知的基础上加上返回通知&异常通知&环绕通知

代码:

package com.cn.spring.aop.impl;//加减乘除的接口类public interface ArithmeticCalculator {    int add(int i, int j);    int sub(int i, int j);    int mul(int i, int j);    int div(int i, int j);}
package com.cn.spring.aop.impl;import org.springframework.stereotype.Component;//实现类@Componentpublic class ArithmeticCalculatorImpl implements ArithmeticCalculator {    @Override    public int add(int i, int j) {        int result = i + j;        return result;    }    @Override    public int sub(int i, int j) {        int result = i - j;        return result;    }    @Override    public int mul(int i, int j) {        int result = i * j;        return result;    }    @Override    public int div(int i, int j) {        int result = i / j;        return result;    }}
package com.cn.spring.aop.impl;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.*;import org.springframework.stereotype.Component;import java.util.Arrays;import java.util.List;//把这个类声明为一个切面:首先需要把该类放入到IOC容器中,在声明为一个切面@Aspect@Componentpublic class LoggingAspect {    //声明该方法是一个前置通知:在目标方法开始之前执行    @Before("execution(public int ArithmeticCalculator.*(int, int))")    public void beforeMethod(JoinPoint joinPoint) {        String methodName = joinPoint.getSignature().getName();        List args = Arrays.asList(joinPoint.getArgs());        System.out.println("The method " +  methodName + " begins with " + args);    }    //后置通知:在目标方法执行后(无论是否发生异常),执行的通知    //在后置通知中还不能访问目标方法执行的结果    @After("execution(public int ArithmeticCalculator.*(int, int))")    public void afterMethod(JoinPoint joinPoint) {        String methodName = joinPoint.getSignature().getName();        List args = Arrays.asList(joinPoint.getArgs());        System.out.println("The method " +  methodName + " ends with " + args);    }    /**     * 在方法正常结束后执行的代码     * 返回通知是可以访问到方法的返回值     * @param joinPoint     */    @AfterReturning(value = "execution(public int ArithmeticCalculator.*(int, int))",    returning = "result")    public void afterReturning(JoinPoint joinPoint, Object result) {        String methodName = joinPoint.getSignature().getName();        List args = Arrays.asList(joinPoint.getArgs());        System.out.println("The method  ends witd " + result);    }    //在目标方法出现异常时会执行的代码    //可以访问到异常对象;且可以指定在出现特定异常时再执行通知代码    @AfterThrowing(value = "execution(public int ArithmeticCalculator.*(int, int))",            throwing = "ex")    public void afterReturning(JoinPoint joinPoint, Exception ex) {        String methodName = joinPoint.getSignature().getName();        System.out.println("The method " +  methodName + " occures exception with: " + ex);    }    /**     * 环绕通知需要携带ProceedingJoinPoint类型的参数     * 环绕通知类似于动态代理的全过程:ProceedingJoinPoint类型的参数可以决定是否执行目标方法     * 且环绕通知必须有返回值,返回值为目标方法的返回值     * @param proceedingJoinPoint     */    @Around("execution(public int ArithmeticCalculator.*(int, int))")    public Object aroundMethod(ProceedingJoinPoint proceedingJoinPoint) {        Object result = null;        String methodName = proceedingJoinPoint.getSignature().getName();        //执行目标方法        try {            //前置通知            System.out.println("The method " +  methodName + " begins with " + Arrays.asList(proceedingJoinPoint.getArgs()));            result = proceedingJoinPoint.proceed();            //返回通知            System.out.println("The method ends with " + result);        } catch (Throwable throwable) {            //异常通知            System.out.println("The method " +  methodName + " occures exception with: " + throwable);            throw new RuntimeException(throwable);        }        //后置通知        System.out.println("The method " +  methodName + " ends");        return result;    }}
package com.cn.spring.aop.impl;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main {    public static void main(String[] args) {        //1.创建Spring的IOC容器        ApplicationContext ctx = new ClassPathXmlApplicationContext("17-1.xml");        //2.从IOC容器中huo获取bean的实例        ArithmeticCalculator arithmeticCalculator = ctx.getBean(ArithmeticCalculator.class);        //3.使用bean        int result = arithmeticCalculator.add(3, 6);        System.out.println("result:" + result);        //result = arithmeticCalculator.div(3, 0);       // System.out.println("result:" + result);    }}

 

转载于:https://www.cnblogs.com/jecyhw/p/4591730.html

你可能感兴趣的文章
Java 求字符串中出现频率最高字符
查看>>
ARM Cortex-M3 异常优先级以及CMSIS RTOS RTX的中断优先级
查看>>
CodeFirst-Section1之小例子
查看>>
Scikit-learn的kmeans聚类
查看>>
MySQL基础(创建库,创建表,添加数据)
查看>>
git 提交丢失Warning, you are leaving 2 commits behind,
查看>>
3、使用SWFUpload使异异步上传文件
查看>>
HDOJ 2136 Largest prime factor
查看>>
Recommended add-ons/plugins for Microsoft Visual Studio [closed]
查看>>
怎么使用 ab.exe 测试多个url。 how to use ab.exe test many url
查看>>
U3D协程Coroutine之WWW与Update()的并行测试
查看>>
二十六.职责链模式
查看>>
这40张图送给单身程序员,情人节请一笑而过!
查看>>
使用VisualStudio发布ASP.NET网站
查看>>
zprofiler三板斧解决cpu占用率过高问题(转载)
查看>>
php替换url参数实现商品筛选效果
查看>>
Java 多线程_优先级
查看>>
js实现图片上传及预览---------------------->>兼容ie6-8 火狐以及谷歌
查看>>
WCF4.0 –- RESTful WCF Services (1) (入门)
查看>>
开源管理系统OSSIM设置 语言为中文简体
查看>>