zoukankan      html  css  js  c++  java
  • a simple example for spring AOP

    /**
    * Created by Administrator on 2015/11/25.
    * a interface
    */
    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);
    }



    public class ArithmeticCalculatorImpl implements ArithmeticCalculator {
    public ArithmeticCalculatorImpl(){}//无参数构造器
    @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;
    }
    }



    public class LoggingAspect {
    //前置通知。
    public void beforeMethod(JoinPoint joinPoint){
    String methodname=joinPoint.getSignature().getName();
    List<Object> args= Arrays.asList(joinPoint.getArgs());
    System.out.println("The Method name is "+methodname+" args is:"+args);
    }

    //后置通知。
    public void AfterMethod(JoinPoint joinPoint){
    String methodname=joinPoint.getSignature().getName();
    System.out.println("The Method "+methodname+" ends!");
    }

    //返回通知,可以访问到方法的返回值。
    public void afterReturning(JoinPoint joinPoint,Object result){
    String methodname=joinPoint.getSignature().getName();
    System.out.println("The Method "+methodname+" end with:"+result);
    }

    //异常通知
    public void afterThrowing(JoinPoint joinPoint,Exception e){
    String methodname=joinPoint.getSignature().getName();
    System.out.println("The Method "+methodname+" occurs excetion:"+e);
    }
    }



    public class Main {
    public static void main(String[]args){
    //貌似java的自动代理机制只能代理接口里面的方法。有待验证。AOP好像也只能代理接口里面的方法
    //java的动态代理是要代理一大堆类,用类你怎么实现这个功能呢
    ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-config.xml");
    //不明白为什么一定要用接口来接受bean的实例,换了实现类会跑异常
    ArithmeticCalculator arithmeticCalculator=(ArithmeticCalculator)ctx.getBean("arithmeticCalculatorImpl");
    arithmeticCalculator.add(100, 20);
    arithmeticCalculator.div(9, 3);
    }
    }


    spring里面的配置如下:
    <!--配置一个普通的bean-->
    <bean id="arithmeticCalculatorImpl" class="Spring_AOP.Aophelloworld.ArithmeticCalculatorImpl"></bean>

    <!--配置一个普通的bean-->
    <bean id="loggingAspect" class="Spring_AOP.Aophelloworld.LoggingAspect"></bean>

    <!--配置Aop信息-->
    <aop:config>
    <!--配置切面表达式-->
    <!--第一个*是public int 表示任意返回类型,接着是全类名.方法(int,int),这里第二个*表示
    所有的方法,(..)表示任意类型的参数-->
    <aop:pointcut id="pointcut" expression="execution(* Spring_AOP.Aophelloworld.ArithmeticCalculator.*(..))"/>
    <!--配置切面通知-->
    <aop:aspect ref="loggingAspect" order="1"><!--order用来配置切面优先级-->
    <aop:before method="beforeMethod" pointcut-ref="pointcut"/>
    <aop:after-returning method="afterReturning" pointcut-ref="pointcut" returning="result"/>
    <aop:after method="AfterMethod" pointcut-ref="pointcut"/>
    <aop:after-throwing method="afterThrowing" pointcut-ref="pointcut" throwing="e"/>
    </aop:aspect>
    </aop:config>
  • 相关阅读:
    Jupyter Notebook 编辑器美化
    Python3 与 C# 基础语法对比(List、Tuple、Dict、Set专栏)
    Ubuntu 18.04 安装微信(附企业微信)
    区块链概念
    Python3 与 C# 基础语法对比(String专栏)
    用Python3、NetCore、Shell分别开发一个Ubuntu版的定时提醒(附NetCore跨平台两种发布方式)
    ArchLinux 设置时间同步和硬件时间同步错误 No usable clock interface found
    ArchLinux dwm的安装和配置
    POJ-1182 食物链 并查集(互相关联的并查集写法)
    POJ-2236 Wireless Network 并查集
  • 原文地址:https://www.cnblogs.com/41ZZY/p/5330363.html
Copyright © 2011-2022 走看看