`
zhjb2000
  • 浏览: 58345 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
文章分类
社区版块
存档分类
最新评论

AOP示例(续)

阅读更多

上一篇演示了,Spring得前通知、后通知、环绕通知,仔细想来前通知、后通知和异常通知,都应该是居于环绕通知开发的,想想如果都能控制目标函数是否能执行,那么要在目标函数执行前后,或者目标函数执行过程中发生异常后进行一些处理,那不是太easy了吗?,按照猜想,前通知、后通知,异常通知 应该是重写invoke方法,在invoke方法里面先执行before在执行proceed就是前通知,先执行proceed在执行afterReturning就是后通知,用在try 里面执行invoke方法就是异常通知。我下面来看spring的源码进行验证

  1. /*
  2. *Copyright2002-2007theoriginalauthororauthors.
  3. *
  4. *LicensedundertheApacheLicense,Version2.0(the"License");
  5. *youmaynotusethisfileexceptincompliancewiththeLicense.
  6. *YoumayobtainacopyoftheLicenseat
  7. *
  8. *http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. *Unlessrequiredbyapplicablelaworagreedtoinwriting,software
  11. *distributedundertheLicenseisdistributedonan"ASIS"BASIS,
  12. *WITHOUTWARRANTIESORCONDITIONSOFANYKIND,eitherexpressorimplied.
  13. *SeetheLicenseforthespecificlanguagegoverningpermissionsand
  14. *limitationsundertheLicense.
  15. */
  16. packageorg.springframework.aop.framework.adapter;
  17. importjava.io.Serializable;
  18. importorg.aopalliance.intercept.MethodInterceptor;
  19. importorg.aopalliance.intercept.MethodInvocation;
  20. importorg.springframework.aop.MethodBeforeAdvice;
  21. importorg.springframework.util.Assert;
  22. /**
  23. *Interceptortowrapam{@linkorg.springframework.aop.MethodBeforeAdvice}.
  24. *UsedinternallybytheAOPframework;applicationdevelopersshouldnotneed
  25. *tousethisclassdirectly.
  26. *
  27. *@authorRodJohnson
  28. */
  29. publicclassMethodBeforeAdviceInterceptorimplementsMethodInterceptor,Serializable{
  30. privateMethodBeforeAdviceadvice;
  31. /**
  32. *CreateanewMethodBeforeAdviceInterceptorforthegivenadvice.
  33. *@paramadvicetheMethodBeforeAdvicetowrap
  34. */
  35. publicMethodBeforeAdviceInterceptor(MethodBeforeAdviceadvice){
  36. Assert.notNull(advice,"Advicemustnotbenull");
  37. this.advice=advice;
  38. }
  39. publicObjectinvoke(MethodInvocationmi)throwsThrowable{
  40. this.advice.before(mi.getMethod(),mi.getArguments(),mi.getThis());
  41. returnmi.proceed();
  42. }
  43. }
  1. /*
  2. *Copyright2002-2007theoriginalauthororauthors.
  3. *
  4. *LicensedundertheApacheLicense,Version2.0(the"License");
  5. *youmaynotusethisfileexceptincompliancewiththeLicense.
  6. *YoumayobtainacopyoftheLicenseat
  7. *
  8. *http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. *Unlessrequiredbyapplicablelaworagreedtoinwriting,software
  11. *distributedundertheLicenseisdistributedonan"ASIS"BASIS,
  12. *WITHOUTWARRANTIESORCONDITIONSOFANYKIND,eitherexpressorimplied.
  13. *SeetheLicenseforthespecificlanguagegoverningpermissionsand
  14. *limitationsundertheLicense.
  15. */
  16. packageorg.springframework.aop.framework.adapter;
  17. importjava.io.Serializable;
  18. importorg.aopalliance.intercept.MethodInterceptor;
  19. importorg.aopalliance.intercept.MethodInvocation;
  20. importorg.springframework.aop.AfterAdvice;
  21. importorg.springframework.aop.AfterReturningAdvice;
  22. importorg.springframework.util.Assert;
  23. /**
  24. *Interceptortowrapam{@linkorg.springframework.aop.AfterReturningAdvice}.
  25. *UsedinternallybytheAOPframework;applicationdevelopersshouldnotneed
  26. *tousethisclassdirectly.
  27. *
  28. *@authorRodJohnson
  29. */
  30. publicclassAfterReturningAdviceInterceptorimplementsMethodInterceptor,AfterAdvice,Serializable{
  31. privatefinalAfterReturningAdviceadvice;
  32. /**
  33. *CreateanewAfterReturningAdviceInterceptorforthegivenadvice.
  34. *@paramadvicetheAfterReturningAdvicetowrap
  35. */
  36. publicAfterReturningAdviceInterceptor(AfterReturningAdviceadvice){
  37. Assert.notNull(advice,"Advicemustnotbenull");
  38. this.advice=advice;
  39. }
  40. publicObjectinvoke(MethodInvocationmi)throwsThrowable{
  41. ObjectretVal=mi.proceed();
  42. this.advice.afterReturning(retVal,mi.getMethod(),mi.getArguments(),mi.getThis());
  43. returnretVal;
  44. }
  45. }
  1. /*
  2. *Copyright2002-2007theoriginalauthororauthors.
  3. *
  4. *LicensedundertheApacheLicense,Version2.0(the"License");
  5. *youmaynotusethisfileexceptincompliancewiththeLicense.
  6. *YoumayobtainacopyoftheLicenseat
  7. *
  8. *http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. *Unlessrequiredbyapplicablelaworagreedtoinwriting,software
  11. *distributedundertheLicenseisdistributedonan"ASIS"BASIS,
  12. *WITHOUTWARRANTIESORCONDITIONSOFANYKIND,eitherexpressorimplied.
  13. *SeetheLicenseforthespecificlanguagegoverningpermissionsand
  14. *limitationsundertheLicense.
  15. */
  16. packageorg.springframework.aop.framework.adapter;
  17. importjava.lang.reflect.InvocationTargetException;
  18. importjava.lang.reflect.Method;
  19. importjava.util.HashMap;
  20. importjava.util.Map;
  21. importorg.aopalliance.intercept.MethodInterceptor;
  22. importorg.aopalliance.intercept.MethodInvocation;
  23. importorg.apache.commons.logging.Log;
  24. importorg.apache.commons.logging.LogFactory;
  25. importorg.springframework.aop.AfterAdvice;
  26. importorg.springframework.util.Assert;
  27. /**
  28. *Interceptortowrapanafter-throwingadvice.
  29. *
  30. *<p>Thesignaturesonhandlermethodsonthe<code>ThrowsAdvice</code>
  31. *implementationmethodargumentmustbeoftheform:<br>
  32. *
  33. *<code>voidafterThrowing([Method,args,target],ThrowableSubclass);</code>
  34. *
  35. *<p>Onlythelastargumentisrequired.
  36. *
  37. *<p>Someexamplesofvalidmethodswouldbe:
  38. *
  39. *<preclass="code">publicvoidafterThrowing(Exceptionex)</pre>
  40. *<preclass="code">publicvoidafterThrowing(RemoteException)</pre>
  41. *<preclass="code">publicvoidafterThrowing(Methodmethod,Object[]args,Objecttarget,Exceptionex)</pre>
  42. *<preclass="code">publicvoidafterThrowing(Methodmethod,Object[]args,Objecttarget,ServletExceptionex)</pre>
  43. *
  44. *<p>ThisisaframeworkclassthatneednotbeuseddirectlybySpringusers.
  45. *
  46. *@authorRodJohnson
  47. *@authorJuergenHoeller
  48. */
  49. publicclassThrowsAdviceInterceptorimplementsMethodInterceptor,AfterAdvice{
  50. privatestaticfinalStringAFTER_THROWING="afterThrowing";
  51. privatestaticfinalLoglogger=LogFactory.getLog(ThrowsAdviceInterceptor.class);
  52. privatefinalObjectthrowsAdvice;
  53. /**Methodsonthrowsadvice,keyedbyexceptionclass*/
  54. privatefinalMapexceptionHandlerMap=newHashMap();
  55. /**
  56. *CreateanewThrowsAdviceInterceptorforthegivenThrowsAdvice.
  57. *@paramthrowsAdvicetheadviceobjectthatdefinestheexception
  58. *handlermethods(usuallya{@linkorg.springframework.aop.ThrowsAdvice}
  59. *implementation)
  60. */
  61. publicThrowsAdviceInterceptor(ObjectthrowsAdvice){
  62. Assert.notNull(throwsAdvice,"Advicemustnotbenull");
  63. this.throwsAdvice=throwsAdvice;
  64. Method[]methods=throwsAdvice.getClass().getMethods();
  65. for(inti=0;i<methods.length;i++){
  66. Methodmethod=methods[i];
  67. if(method.getName().equals(AFTER_THROWING)&
  68. //m.getReturnType()==null&&
  69. (method.getParameterTypes().length==1||method.getParameterTypes().length==4)&
  70. Throwable.class.isAssignableFrom(method.getParameterTypes()[method.getParameterTypes().length-1])
  71. ){
  72. //Haveanexceptionhandler
  73. this.exceptionHandlerMap.put(method.getParameterTypes()[method.getParameterTypes().length-1],method);
  74. if(logger.isDebugEnabled()){
  75. logger.debug("Foundexceptionhandlermethod:"+method);
  76. }
  77. }
  78. }
  79. if(this.exceptionHandlerMap.isEmpty()){
  80. thrownewIllegalArgumentException(
  81. "Atleastonehandlermethodmustbefoundinclass["+throwsAdvice.getClass()+"]");
  82. }
  83. }
  84. publicintgetHandlerMethodCount(){
  85. returnthis.exceptionHandlerMap.size();
  86. }
  87. /**
  88. *Determinetheexceptionhandlemethod.Canreturnnullifnotfound.
  89. *@paramexceptiontheexceptionthrown
  90. *@returnahandlerforthegivenexceptiontype
  91. */
  92. privateMethodgetExceptionHandler(Throwableexception){
  93. ClassexceptionClass=exception.getClass();
  94. if(logger.isTraceEnabled()){
  95. logger.trace("Tryingtofindhandlerforexceptionoftype["+exceptionClass.getName()+"]");
  96. }
  97. Methodhandler=(Method)this.exceptionHandlerMap.get(exceptionClass);
  98. while(handler==null&&!exceptionClass.equals(Throwable.class)){
  99. exceptionClass=exceptionClass.getSuperclass();
  100. handler=(Method)this.exceptionHandlerMap.get(exceptionClass);
  101. }
  102. if(handler!=null&&logger.isDebugEnabled()){
  103. logger.debug("Foundhandlerforexceptionoftype["+exceptionClass.getName()+"]:"+handler);
  104. }
  105. returnhandler;
  106. }
  107. publicObjectinvoke(MethodInvocationmi)throwsThrowable{
  108. try{
  109. returnmi.proceed();
  110. }
  111. catch(Throwableex){
  112. MethodhandlerMethod=getExceptionHandler(ex);
  113. if(handlerMethod!=null){
  114. invokeHandlerMethod(mi,ex,handlerMethod);
  115. }
  116. throwex;
  117. }
  118. }
  119. privatevoidinvokeHandlerMethod(MethodInvocationmi,Throwableex,Methodmethod)throwsThrowable{
  120. Object[]handlerArgs;
  121. if(method.getParameterTypes().length==1){
  122. handlerArgs=newObject[]{ex};
  123. }
  124. else{
  125. handlerArgs=newObject[]{mi.getMethod(),mi.getArguments(),mi.getThis(),ex};
  126. }
  127. try{
  128. method.invoke(this.throwsAdvice,handlerArgs);
  129. }
  130. catch(InvocationTargetExceptiontargetEx){
  131. throwtargetEx.getTargetException();
  132. }
  133. }
  134. }

通过spring的源码可以确定刚刚的想法。现在明白了前通知和后通知,异常通知是怎么实现的了,但是问题又来了,怎么在我调用业务方法的前去执行invoke方法呢?看来还要继续研究一下^_^

分享到:
评论

相关推荐

    SpringAOP示例讲解

    **Spring AOP 示例讲解** Spring AOP(Aspect Oriented Programming,面向切面编程)是Spring框架中的一个重要模块,它提供了一种在不修改源代码的情况下,对程序进行功能增强的技术。这种技术允许开发者将关注点从...

    Spring AOP 1.0示例

    在这个“Spring AOP 1.0示例”中,我们重点关注如何在实际项目中应用这一特性。 首先,我们需要了解AOP的基本概念。AOP的核心思想是将那些影响多个类的公共行为(如日志记录)抽取出来,形成独立的模块,称为切面...

    基于realproxy的aop使用示例

    在"基于realproxy的aop使用示例"中,我们可能看到以下步骤: 1. **创建代理类**:首先,我们需要创建一个继承自RealProxy的子类,这个子类负责拦截目标对象的方法调用。在这个子类中,我们可以重写`Invoke`方法,该...

    spring aop简单应用示例

    本示例将深入探讨Spring AOP的基础知识,以及如何在实际应用中使用它。 首先,我们来看"LogProfilter.java",这很可能是实现一个日志拦截器的类。在Spring AOP中,这样的类通常被称为切面(Aspect)。切面是封装了...

    Spring AOP代码示例

    Spring AOP,全称Aspect-Oriented Programming(面向切面编程),是Spring框架的一个重要组成部分。它提供了一种模块化和声明式的方式来处理系统中的交叉关注点,如日志、性能监控、安全性、事务管理等。通过AOP,...

    Emit实现AOP示例

    纯手工打造Emit实现AOP private static void OverrideMethods(TypeBuilder tb, MethodInfo method) { if (!method.IsPublic|| !method.IsVirtual || IsObjectMethod(method)) return; Type[] paramTypes = ...

    Spring的AOP示例DEMO HELLOWORLD

    本示例DEMO "Spring的AOP示例DEMO HELLOWORLD" 将引导我们深入理解Spring AOP的核心概念,并通过一个简单的 HelloWorld 示例来展示其实现过程。 首先,面向切面编程(AOP)是一种编程范式,旨在提高代码的可维护性...

    Spring AOP示例

    这个“Spring AOP示例”包含了一个具体的实践案例,帮助我们更好地理解和应用Spring AOP。 在Spring AOP中,核心概念有以下几个: 1. **切面(Aspect)**:切面是关注点的模块化,比如事务管理就是一个切面。在...

    Sping aop简单示例

    在这个"Spring AOP简单示例"中,我们将深入探讨这五个关键元素,并通过XML配置来实现它们。 首先,**切面(Aspect)**是关注点的模块化,这些关注点定义了跨越多个对象的行为或责任。在Spring AOP中,切面可以是...

    spring aop API示例

    在这个"spring aop API示例"中,我们将深入探讨如何利用Spring AOP的四种通知类型:Before、After、AfterThrowing和Around,以及它们在实际开发中的应用。 1. **Before通知**: 在方法执行前触发,可以用来执行...

    spring famework 基于xml配置aop示例

    本示例将详细阐述如何通过XML配置来实现Spring AOP。 首先,我们需要理解AOP的基本概念。AOP的核心是切面(Aspect),它封装了横切关注点,也就是那些跨越多个对象的业务逻辑。这些关注点通常包括日志、安全检查和...

    SpringAOP:Spring AOP示例

    SpringAOP Spring AOP(面向方面​​的编程)用于模块化“横截面”服务。 用一种简单的方式,我们可以说它是一个旨在拦截某些进程的组件,例如,在执行某个方法时,Spring AOP可以... 以下示例将向您展示其工作原理

    spring2.5.6 aop简单示例

    在本示例中,我们将深入探讨Spring框架2.5.6版本中的面向切面编程(AOP)概念。Spring AOP是Spring框架的核心组件之一,它允许开发者在不修改源代码的情况下,对程序进行横切关注点(如日志、事务管理、性能监控等)...

    aop示例spring 的aop思想解决项目中多次出现的同一个问题

    Spring框架的AOP(面向切面编程)正是为了解决这类问题而设计的。AOP允许我们在不修改原有业务逻辑的情况下,将这些共性功能以一种模块化的方式插入到业务代码中,从而提高了代码的可复用性和可维护性。 首先,理解...

    spring aop示例

    在这个"spring aop示例"中,我们看到了如何使用Spring AOP来实现方法执行前打印方法名和参数的功能。这主要涉及到三个方面:AOP的基本概念、注解的使用以及Spring的自动注入。 首先,AOP的核心概念包括切面(Aspect...

    Spring IOC AOP学习示例

    Spring IOC AOP学习示例代码,包含Spring常用操作示例和所有所需jar文件。参考博客:http://blog.csdn.net/daijin888888/article/details/51735291

    Spring AOP完整例子

    在本教程的示例中,你可能发现了一些配置文件,如`applicationContext.xml`,它们用于装配切面。在Spring XML配置中,我们可以使用`&lt;aop:config&gt;`元素来定义切点表达式,然后使用`&lt;aop:aspect&gt;`元素来声明切面,并将...

    Spring Aop 示例

    以下是一个简单的Spring AOP示例,展示如何使用注解定义切面和通知: ```java // 定义切面 @Aspect @Component public class LoggingAspect { // 定义切入点,匹配所有以'execute'开头的方法 @Pointcut(...

    SSH学习记录(4)-AOP测试

    而压缩包子文件的文件名称列表"SSHibernate"可能是表示这是一个关于Spring、Struts和Hibernate整合的示例项目,特别强调了Hibernate的部分。Hibernate是Java领域的一个持久化框架,用于简化数据库操作。在SSH整合...

    aop例子aop例子

    在IT行业中,面向切面编程(Aspect Oriented Programming,AOP)是一种强大的设计模式,它允许程序员将关注点从核心业务逻辑中分离出来,比如日志记录、事务管理、性能监控等。本例子是一个关于如何在Spring框架中...

Global site tag (gtag.js) - Google Analytics