博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
22Spring基于配置文件的方式配置AOP
阅读量:5146 次
发布时间:2019-06-13

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

直接看代码:

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;//实现类public 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 java.util.Arrays;import java.util.List;public class LoggingAspect {    public void declareJoinPointExpression() {}    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);    }    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);    }    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);    }    public void afterThrowing(JoinPoint joinPoint, Exception ex) {        String methodName = joinPoint.getSignature().getName();        System.out.println("The method " +  methodName + " occures exception with: " + ex);    }    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.aspectj.lang.JoinPoint;import java.util.Arrays;public class ValidationAspect {    public void validateArgs(JoinPoint joinPoint) {        System.out.println("validate:" + Arrays.asList(joinPoint.getArgs()));    }}
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("22-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/4596292.html

你可能感兴趣的文章
多路复用
查看>>
Python数据可视化之Pygal(雷达图)
查看>>
Java学习笔记--字符串和文件IO
查看>>
转 Silverlight开发历程—(画刷与着色之线性渐变画刷)
查看>>
SQL语法(3)
查看>>
在js在添版本号
查看>>
sublime3
查看>>
Exception Type: IntegrityError 数据完整性错误
查看>>
Nuget:Newtonsoft.Json
查看>>
Hdu - 1002 - A + B Problem II
查看>>
每天CookBook之Python-003
查看>>
Android设置Gmail邮箱
查看>>
js编写时间选择框
查看>>
JIRA
查看>>
小技巧——直接在目录中输入cmd然后就打开cmd命令窗口
查看>>
深浅拷贝(十四)
查看>>
HDU 6370(并查集)
查看>>
BZOJ 1207(dp)
查看>>
PE知识复习之PE的导入表
查看>>
HDU 2076 夹角有多大(题目已修改,注意读题)
查看>>