博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring - aop 使用方式总结
阅读量:4513 次
发布时间:2019-06-08

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

方式一:接口

  前置增强      MethodBeforeAdvice
  后置增强      AfterReturningAdvice
  异常抛出增强  ThrowsAdvice
  环绕增强      MethodInterceptor
  注意:还需要在applicationContext.xml文件中进行aop相关的配置
        <aop:config>
        <aop:pointcut expression="execution(public void *User())" id="addoptpointcut"/>
        <aop:advisor advice-ref="logbefore" pointcut-ref="addoptpointcut" />
    </aop:config>
        <aop:config>
        <aop:pointcut expression="execution(* spring_basic.UserService.*(..))" id="userServiceOptPointcut"/>
        <!--
        <aop:advisor advice-ref="exceptionAdvice" pointcut-ref="userServiceOptPointcut" />
         -->
        <aop:advisor advice-ref="logger" pointcut-ref="userServiceOptPointcut" />
    </aop:config>
方式二:注解
  前置增强      @Before("execution(* service.*.*(..))")
  后置增强      @AfterReturning(pointcut="execution(* service.*.*(..))", returning="result")
  异常抛出增强  @AfterThrowing(pointcut="execution(* service.*.*(..))", throwing="ex")
  环绕增强      @Around("execution(* service.*.*(..))")
  注意:还需要在applicationContext.xml文件中添加如下配置
        <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
方式三:Scheme
  首先:添加普通的类,里面按要求编写方法
  public class Logger {
    public void before(JoinPoint jp){
    }
    
    public void after(JoinPoint jp, Object result){
    }
    
    public void aterThrowing(JoinPoint jp, Exception ex){
    }
    
    public Object aroundExecute(ProceedingJoinPoint pjp) throws Throwable{
    }
  }
  其次:在applicationContext.xml中配置aop-aspect相关的配置
        <aop:config>
        <aop:pointcut expression="execution(* service.*.*(..))" id="optpointcut"/>
        
        <aop:aspect ref="mylogger">
            <aop:before method="before" pointcut-ref="optpointcut" />
            <aop:after-returning method="after" returning="result" pointcut-ref="optpointcut" />
            <aop:after-throwing method="aterThrowing" throwing="ex" pointcut-ref="optpointcut" />
            <aop:around method="aroundExecute" pointcut-ref="optpointcut"/>
        </aop:aspect>
        
    </aop:config>

转载于:https://www.cnblogs.com/ws1313123/p/6424773.html

你可能感兴趣的文章
iptables基本原理和规则配置
查看>>
java调用matlab函数
查看>>
IOS自定义仪表盘
查看>>
第5次作业_078_刘玲志
查看>>
ZOJ 1184
查看>>
spring - aop 使用方式总结
查看>>
最后,我想对你说一句:我爱你
查看>>
使用jndi连接数据库
查看>>
Python---- 函数
查看>>
javascript中的函数作用域和声明提前
查看>>
Xcode10升级项目报错library not found for -lstdc++.6.0.9
查看>>
ZOJ-1129-Erdos Numbers
查看>>
java学习第四天 类和变量
查看>>
IDEA中如何添加RunDashboard
查看>>
单例静态继承
查看>>
Android开发:《Gradle Recipes for Android》阅读笔记(翻译)3.2——设置Flavors和Variants...
查看>>
Android零基础入门第36节:Android系统事件的响应
查看>>
POJ 2262 Goldbach's Conjecture
查看>>
自己手动写代码实现数据库连接池
查看>>
领域对象驱动开发:来吧,让我们从对象开始吧
查看>>