Spring.NET.AOP - ExceptionHandlerAdvice不会替换自定义异常 [英] Spring.NET.AOP - ExceptionHandlerAdvice doesnt replace custom exception

查看:194
本文介绍了Spring.NET.AOP - ExceptionHandlerAdvice不会替换自定义异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的第一个,而且我也是Spring.NET的初学者,也是AOP的初学者。

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

我想使用Aspect for Exception Hadling替换,包装和修改我的自定义异常。

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

定义了一些实体和DAO。从方法保存在DAO中我将抛出我的异常。

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

FYI:这是傻样本

实体:

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

DAO: / p>

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类定义在这里:

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

        }
    }
}

在app.config中我定义了 CustomerDao object和 ExceptionHandlerAdvice 对象,它只替代 CustomerException for System.ArgumentException

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

我不确定如果 ExceptionHandlerAdvice 是自动代理,也不如何确定目标。

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

我相信它使用SpEL定义规则,当有一些例外时,它会抛出检查列表。
确定这种类型的异常是列表中的一些我将应用建议。

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.

我使用ref文档第14.3章异常处理,但我找不到这些信息。

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

这里是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>

我的主要问题是如果我调用方法在DAO上保存它抛出异常类型的CustomerException此异常未被替换。为什么?

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));
            }

抛出异常是 CustomerException not ArgumentException

Thrown exception is type of CustomerException not ArgumentException,

此外,当咨询适用时,我还尝试使用DSL定义规则:

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))

但仍然是抛出异常类型的CustomerException。

But still is throw exception type of CustomerException.

感谢您的帮助。

推荐答案

Spring.NET aop为要应用建议的对象动态创建代理。当您执行 var customerDao =(ICustomerDao)context [customerDao]; 时,Spring.NET返回此代理。所以,当正确配置时,您不会得到一个 CustomerDao 实例,但是来自Spring的aop代理实现了 ICustomerDao 界面。该代理拦截对 Save(...)的调用,并应用您的异常处理建议。

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.

但是,你没有配置一个 ProxyFactory ,所以你调用<$ c时不会得到一个代理,而是一个 CustomerDao $ c> var customerDao =(ICustomerDao)context [customerDao]; 。你可以在调试器中检查这个;检查(运行时)类型 customerDao

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.

有几种配置代理工厂的方法;我建议您使用 AdvisorAutoProxy ,但您也可以使用一个简单的 ProxyFactoryObject 或另一个 AutoProxy

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.

使用 AdvisorAutoProxy 时,您必须将以下对象定义添加到配置中:

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" />

这篇关于Spring.NET.AOP - ExceptionHandlerAdvice不会替换自定义异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆