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>
  • 相关阅读:
    composer阿里云短信服务不支持传参为数值--为2017年短信接口,2018阿里云有更新http://www.cnblogs.com/q1104460935/p/8916096.html
    随机生成字符串,数字,手机号,邮箱
    C#: .net序列化及反序列化 [XmlElement(“节点名称”)] [XmlAttribute(“节点属性”)] (上篇)
    自动升级功能
    C# WinForm 设置按纽为透明,使用背景色
    sql server 2000 单主键高效分页存储过程 (支持多字段排序)
    分页存储过程
    C# WinForm 解决子窗体放大后,子窗体图标放大的问题
    Windows 7/8 64位系统 不能注册32位dll 文件的解决方案
    添加ico图标
  • 原文地址:https://www.cnblogs.com/41ZZY/p/5330363.html
Copyright © 2011-2022 走看看