在目标方法执行前后,进行代码增强 (阻止目标方法的执行 )

环绕通知实现任何通知效果。

案例如下:

案例结构:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAASsAAAB1CAIAAACZNVDQAAAR00lEQVR4nO2dW4wb1RnHh4eKl0p9oBKPqFQFQlSpCbFZ3BAopSUqVVMVgRIJNptEsgIRIil3SEp3sxdnE+i0FBIEhIRLcJItpFA5RDSNeEhJopBV0iAkW7shuwvZ9a4Xry9rj73S9GFu5zoz3vV4ztjfTyNl58x3LnbO3+fMsc9/JFUATnQt+ey5n5rHy/dfh56e6FrS0Nak5IgUkVMNrdMt9W1bIirKCxX5PfcYye8GqKqqfvb04tSJvd+c3v/N6f3nDu/cu3k1evrZ04t9aldKjkjRhAiFeIPPChT4nWkgnivwxIkTsVjMIebJm785vV+Z+TRz/sBXn+zeu3k1enriyZu9biQHUKCnCPzONBBvFXj8+PHFixffcMMN9mGfbrn5m9P7M+cPZM4f+OrYHk2B5umnW/gKTMfj2x4+m3ZsSCIq6UTklN715CiSgqB3DCuLEUEUokXqmD0JT6MLYYaZtVrxRoFIekROWZ2W9xLMEiKyzBQY0iTGi+e2r5Y2s94rMlHG3pkWFqOHCtTkt3r1akcFHn30pnOHd371ye6vju35fF/Xs6vuRE+PPnqTbe7Rs3+/55Vt97yyrX+IHZCIkj0tEcV6ES0OtIfxCknJUeM8EdWLY/QkVufihElYbVYbSD1yX4KVTc9ICgwNYLykebw0VpvRsdXMxnoDzRJBgXXHlN+aNWvsFbhl+bUv33/d3s2rtePZVXc+u+rO+VSZjse33XPsSyqdMdciuogLBXImbNSwoH3Mo6HMzuUYZp7i6cQYSLwEopF0mx0DFvTSjFN8lNXzMOoCBaqqRwpE5eeoQFVVP9jwE3TpRVPgNdu/lK56UbrqxWu206pCcRoDPVIgOsaQHUjrvdpFm85lE+arAuf50hAF0sWCAjl4osBYLHYDQm9vr338wY4fEysxqqqa2pOuepGb09V9IDr1SshkVzW6rzmX489CsUKQMlJyxJyqyeR8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sxVsHoPSRRkZHqfiWGUiA3vJZCrGZgLaU/EqxPD7zl+t0fFQYKbARCKLDrmd9eTpcvT5ZGJssjk+WRtG42YTcF5cKbYtVrCR4A6okQCvzTU7/7eqJ0aaL0tXEsoDBQIBAkhFDg80+sIg4tHbV7cQ0oEAgSQigQAFoWUCAA+AkoEAD8BBQIAH4CCgQAPwEFAoCfgAIBwE/8V6AkSd+7+mr7Q5KY7azrr6KE9XoRtmFAPRBCgT94OG5/eKVActODkB1d2IYB9aCVFZiSI5GIoL0bfsHTKgihQDewsi5IgdrmAjunCD8BBbYKQihwvjGoAtmbfJD0iCzb7lNn9HkXljOUOwu5H4qRHYtHNx4aZUbMHUY2W5+w/baMFw4EA1EUaA50zNHPSYF2xirYnj9KbY4KdLac4dZu40xDb1cnyuRt/6UywmAZcERR4LwwFMgzViHSjVO2txF3DKRP8XSurQsnO9vNhVcXboHByEhYSwABQxQFEmOg6sYnxk6BLCsk/RSZtaEzx3oqkPaPULmfCw51OVtImUGgwyAiigIZ6Y4+MTazUOT2jDULxYogx0Br9d/Ocoa4qWPVbuNMQ7u5uFAgM2NKlhNUFiA4iKJAegzUr9r5xLhYiUEcXrCVGKQIewVyLWcSRDF07bzszMUTsky2CQ1r1YW51AMEBVEUyL40H58YPvbfPDjPQmsFBiXAGSEUSPwAbQFrMwQpOcJZsWSFUpdBgYDn+K9AG+blE4ODTNu48tOncbRYQIGA5witQABoekCBAOAnoEAA8BNQIAD4CSgQAPzk/1mhXh80lFn1AAAAAElFTkSuQmCC" alt="" />

第一步:编写代理类。

MyAspect.java

	//这里必须要抛异常
public Object around( ProceedingJoinPoint proceedingJoinPoint) throws Throwable
{
System.out.print("环绕通知 方法前执行");
Object result=proceedingJoinPoint.proceed();
System.out.print("环绕通知 方法后执行");
return result; }

第二步:写ApplicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="OrderService" class="cn.itcast.spring.c_aop.impl.OrderServiceimpl" /> <!-- AspectJ AOP -->
<!-- 配置目标 -->
<bean id="CustomerService" class="cn.itcast.spring.d_aspectj.CustomerService"></bean>
<!-- 配置切面类 -->
<bean id="MyAspect" class="cn.itcast.spring.d_aspectj.MyAspect"></bean>
<aop:config>
<!-- ref引用切面类 -->
<aop:aspect ref="MyAspect"> <aop:pointcut expression="execution(* cn.itcast.spring.d_aspectj.CustomerService.*(..))" id="mypointcut2"/> <aop:around method="around" pointcut-ref="mypointcut2"/> </aop:aspect> </aop:config> </beans>

第三步 写Junit测试的代码:

	@Test
public void testaround()
{ customerService.delete(); }

第四步  输出结果:

环绕通知 方法前执行
this is delete
环绕通知 方法后执行

14Spring_AOP编程(AspectJ)_环绕通知的更多相关文章

  1. 12Spring_AOP编程(AspectJ)_前置通知

    接下里的博客会一篇一篇的讲解每一个通知.其实AOP_AspectJ的编程与传统的AOP的编程的最大的区别就是写一个Aspect 支持多个Advice和多个PointCut .而且我们写AOP_Aspc ...

  2. 16Spring_AOP编程(AspectJ)_最终通知

  3. 13Spring_AOP编程(AspectJ)_后置通知

    后置通知和前置通知差不多.最大的特点是因为后置通知是运行在目标方法之后的,所以他可以拿到目标方法的运行的结果. 给出案例: 案例结构图:

  4. 15Spring_AOP编程(AspectJ)_抛出通知

  5. 11Spring_AOP编程(AspectJ)_概述

    AspectJ 是一个框架 (第三方AOP框架 ),提供切面编程 ,编写一个Aspect 支持多个Advice和多个PointCut .对比前一种提到的传统的Aop编程,AspctJ更加的常用.Asp ...

  6. 19Spring_AOP编程(AspectJ)_使用@Pointcut注解来定义切点

    我们之前的方式是采用 @AfterReturning(value="execution(* com.guigu.shen.anotion.UserDaoImpl.*(..))",r ...

  7. spring框架应用系列四:切面编程(环绕通知与前后置通知区别)

    切面编程(环绕通知与前后置通知区别) 本文系作者原创,转载请注明出处:http://www.cnblogs.com/further-further-further/p/7867034.html 解决问 ...

  8. AOP 环绕通知 (Schema-base方式) 和 AspectJ方式在通知中获取切点的参数

    环绕通知(Schema- base方式) 1.把前置通知和后置通知都写到一个通知中,组成了环绕通知 2.实现步骤: 2.1 新建一个类实现 MethodInterceptor 接口 public cl ...

  9. 18Spring_AOP编程(AspectJ)_AspectJ的各种通知总结

    小结: 前置通知(权限控制). 后置通知 ---- 不怎么用 环绕通知(权限控制. 性能监控. 缓存技术 ) 异常通知 (发生异常后, 记录错误日志 ) 最终通知 (释放资源 ) 环绕通知 是取代任何 ...

随机推荐

  1. Azure SQL Database 时间点还原(Point in Time Restore)功能

      微软中国TechNet 7 Oct 2014 9:17 PM Comments 0 Likes 原文地址:http://blogs.technet.com/b/azuretw/archive/20 ...

  2. Microsoft Dynamics CRM 前瑞开发

    做CRM开发最大的感受就是其前瑞开发过程中,调试起来比较麻烦,需要做一些断点还要配制一些浏览器设置,对新手来说比较困难.还有就是对REST调试,经常为了调试一个正确的结果而花费大量的时间.现在推荐一个 ...

  3. Q:关于Outlook for CRM 中预览记录窗体的设置

    问题: 如何在Outlook for CRM中,将实体记录的预览窗口的信息做调整? 解决方案: 在Outlook里,在打开实体后选择View=>Customize Reading Pane,这里 ...

  4. Java解析XML三种常用方法

    1.使用DOM方式解析: package com.wzh.dom; import java.util.Iterator; import javax.xml.parsers.DocumentBuilde ...

  5. 设计模式:模版模式(Template Pattern)-转

    模版模式 又叫模板方法模式,在一个方法中定义一个算法的骨架,而将一些步骤延迟到子类中.模板方法使得子类可以在不改变算法结构的情冴下,重新定义算法中的某些步骤. 我们使用冲泡咖啡和冲泡茶的例子 加工流程 ...

  6. float类型转对象 对象转float类型(一)

    //float类型转化为对象CGFloat fValue = 1.f;NSNumber *objNo = [NSNumber numberWithFloat:fValue];数值.BOOL型都可以转成 ...

  7. iOS开发笔记10:圆点缩放动画、强制更新、远程推送加语音提醒及UIView截屏

    1.使用CAReplicatorLayer制作等待动画 CALayer+CABasicAnimation可以制作很多简单的动画效果,之前的博客中介绍的“两个动画”,一个是利用一张渐变色图片+CABas ...

  8. WebService学习总结(二)——WebService相关概念介绍

    一.WebService是什么? 1. 基于Web的服务:服务器端整出一些资源让客户端应用访问(获取数据) 2. 一个跨语言.跨平台的规范(抽象) 3. 多个跨平台.跨语言的应用间通信整合的方案(实际 ...

  9. 关于String StringBuffer StringBuilder

    0. String对象的创建       1.关于类对象的创建,很普通的一种方式就是利用构造器,String类也不例外:String s=new String("Hello world&qu ...

  10. java 接收 char字符型

    import java.io.BufferedReader;import java.io.InputStreamReader;import java.util.Scanner; public clas ...