Spring.NET.AOP - ExceptionHandlerAdvice doesnt replace custom exception

后端 未结 1 698
滥情空心
滥情空心 2020-12-21 10:14

this is my first and also I am beginner in Spring.NET and also AOP.

I would like use Aspect for Exception Hadling for replacing, wrap and modify my custom exception

相关标签:
1条回答
  • 2020-12-21 10:43

    Spring.NET aop dynamically creates a proxy for the objects you want to apply advice to. Spring.NET returns this proxy when you do var customerDao = (ICustomerDao)context["customerDao"];. So when configured correctly, you don't get a CustomerDao instance, but an aop proxy from Spring implementing the ICustomerDao interface. This proxy intercepts the call to Save(...) and applies your exception handling advice.

    However, you haven't configured a ProxyFactory, so you will not get a proxy, but a CustomerDao instance when calling var customerDao = (ICustomerDao)context["customerDao"];. You can check this in the debugger; inspect the (runtime) type of customerDao.

    There are several methods to configure the proxy factory; I'd advise you to use an AdvisorAutoProxy for your case, but you can also configure it manually for each object using a plain ProxyFactoryObject or another AutoProxy.

    When using an AdvisorAutoProxy, you have to add the following object definitions to your configuration:

    <object id="ExceptionAdvisorForSaveMethods" 
            type="Spring.Aop.Support.RegularExpressionMethodPointcutAdvisor, Spring.Aop">
      <property name="advice" ref="exceptionHandlerAdvice"/>
      <property name="patterns">
        <list>
          <value>.*Save.*</value>
        </list>
      </property>
    </object>
    
    <object id="ProxyCreator" 
            type="Spring.Aop.Framework.AutoProxy.DefaultAdvisorAutoProxyCreator, Spring.Aop" />
    
    0 讨论(0)
提交回复
热议问题