Spring.NET.AOP - ExceptionHandlerAdvice doesnt rep

2019-01-28 09:03发布

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 exceptions.

First I defined some entity and DAO. From method Save in DAO I will throw my exception.

FYI: This is silly sample

Entity:

namespace ExceptionHandlingTutorial.Entities
{
    public class Customer
    {
        public long Id { get; set; }
        public string Name { get; set; }
    }
}

DAO:

namespace ExceptionHandlingTutorial.Dao
{
    public interface ICustomerDao
    {
        void Save(Customer customer);
    }

    public class CustomerDao:ICustomerDao
    {
        #region Implementation of ICustomerDao

        public void Save(Customer customer)
        {
            throw new CustomerException(
                string.Format("Customer with id {0} already exist in repository",customer.Id));
        }

        #endregion
    }
}

CustomException class definition is here:

namespace ExceptionHandlingTutorial
{
    public class CustomerException : Exception
    {
        public CustomerException(string msg)
            : base(msg)
        {

        }
    }
}

In app.config I defined CustomerDao object and ExceptionHandlerAdvice object which only replace CustomerException for System.ArgumentException.

I am not sure if ExceptionHandlerAdvice is auto-proxy and also I don’t how it identified targets.

I believe that it uses SpEL for define rules and when there is some exception it throws check list. Ok this type of exception is in list some I will apply advice.

Can anybody explain to me how this aspect identified targets? For example I would like apply this aspect only for a few objects not all.

I use ref documentation Chapter 14.3 Exception handling but I couldnt find these information.

Here is app.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <configSections>
    <sectionGroup name="spring">
      <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
      <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core"/>
    </sectionGroup>
  </configSections>

  <spring>

    <context>
      <resource uri="config://spring/objects"/>
    </context>

    <objects xmlns="http://www.springframework.net">

      <object id="customerDao" 
              type="ExceptionHandlingTutorial.Dao.CustomerDao, ExceptionHandlingTutorial"/>

      <object id="exceptionHandlerAdvice" 
              type="Spring.Aspects.Exceptions.ExceptionHandlerAdvice, Spring.Aop">

        <property name="ExceptionHandlers">
          <list>
            <value>on exception name CustomerException replace System.ArgumentException 'Something'</value>
          </list>
        </property>

      </object>

    </objects>

  </spring>
</configuration>

My main problem is that if I call method Save on DAO it throw exception type of CustomerException this exception is not replaced. Why?

try
            {
                var context = ContextRegistry.GetContext();
                var customerDao = (ICustomerDao)context["customerDao"];

                customerDao.Save(new Customer { Id = 1, Name = "Customer_1" });
            }
            catch (Exception ex)
            {

                Console.WriteLine(string.Format("Exception type: {0}\nException message: {1}\n",
                    ex.GetType(),ex.Message));
            }

Thrown exception is type of CustomerException not ArgumentException,

Also I tried use DSL for defined rules when advice apply:

on exception (#e is T(ExceptionHandlingTutorial.CustomerException) translate new System.ArgumentException('XChange, Method Name'+ #method.Name, #e))

But still is throw exception type of CustomerException.

Thank you for help.

1条回答
forever°为你锁心
2楼-- · 2019-01-28 09:19

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" />
查看更多
登录 后发表回答