• Spring之基于Schema形式的AOP实现


    一、创建LogSchema.java:

    package org.ruangong.aop;

    import java.lang.reflect.Method;

    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.ProceedingJoinPoint;

    public class LogSchema {

    public void afterReturning(JoinPoint jp,Object returnValue) throws Throwable {
    // TODO Auto-generated method stub
    System.out.println("(Schema)后置通知....目标对象:"+jp.getThis()+",调用的方法:"+jp.getSignature().getName()+",方法的参数的个数:"+jp.getArgs().length+",返回值:"+returnValue);
    }

    public void before(){
    System.out.println("(Schema)前置通知...");
    }
    public void whenException(JoinPoint jp,NullPointerException e){
    System.out.println("(Schema)异常通知..."+e.getMessage());
    }
    public Object around(ProceedingJoinPoint jp){
    System.out.println("(Scheam)环绕通知:前置通知。。。。。。");
    Object result = null;
    try{
    result = jp.proceed();
    System.out.println("(Scheam)环绕通知:后置通知。。。。。。");
    }catch(Throwable e){
    System.out.println("(Scheam)环绕通知:异常通知。。。。。。");
    }
    return result;
    }
    }

      在这里一定要注意,利用注解或者Schema实现aop一定要把后置通知参数改为JoinPoint jp。

    在applicationContext.xml文件中添加:

    <!-- Schema实现通知 -->
    <bean id="logSchema" class="org.ruangong.aop.LogSchema"></bean>

    <aop:config>
    <aop:pointcut expression="execution(public void org.ruangong.service.impl.StudentServiceImpl.addStudent(org.ruangong.entity.Student))" id="pcSchema"/>
    <!-- <aop:advisor advice-ref="logSchema" pointcut-ref="pcSchema"/> -->
    <!-- Schema方式 -->
    <aop:aspect ref="logSchema">
    <aop:before method="before" pointcut-ref="pcSchema"/>
    <aop:after-returning method="afterReturning" returning="returnValue" pointcut-ref="pcSchema"/>
    <aop:after-throwing method="whenException" throwing="e" pointcut-ref="pcSchema"/>
    <!-- 环绕通知 -->
    <aop:around method="around" pointcut-ref="pcSchema"/>
    </aop:aspect>
    </aop:config>

      实现测试类:

  • 相关阅读:
    20175216 数据结构(选做)
    20175216 《Java程序设计》第1周学习总结
    20175216 MyCP(课下作业)
    WPF 4 DataGrid 控件(进阶篇一)
    InstallShield 通过VBS操作IIS
    WPF 4 DataGrid 控件(自定义样式篇)
    INNO 实现Sql数据库操作
    Wix学习整理(7)——在开始菜单中为HelloWorld添加卸载快捷方式
    Wix学习整理(5)——安装时填写注册表
    Wix学习整理(4)——关于WiX文件格式和案例HelloWorld的分析
  • 原文地址:https://www.cnblogs.com/jccjcc/p/13987178.html
Copyright © 2020-2023  润新知