Spring.Net拦截器在从基类继承时不会触发

时间:2014-01-20 16:25:31

标签: c# asp.net-mvc inheritance aop spring.net

我在使用Spring.Net的AOP时遇到了麻烦。如果给定的方法是基类的继承方法,似乎拦截器不会触发。

但是,如果我从基础覆盖继承的方法,则会触发拦截器。

有没有办法让拦截器在没有覆盖基座的情况下发射?

到目前为止我所拥有的内容:( this example的修改版本):

public class HomeController : BaseController
{
    //**Uncommenting this will work**
    //public override void Delete()
    //{
    //    base.Delete();
    //}
}

public class BaseController : Controller
{
    [SetMethodInfoAsMessage]
    public virtual void Delete()
    {

    }
}

public class SetMethodInfoAsMessageAdvice : IMethodBeforeAdvice
{
    public void Before(MethodInfo method, object[] args, object target)
    {
        //Do something
    }
}

public class SetMethodInfoAsMessageAttribute : Attribute
{
}    

使用以下配置:

<?xml version="1.0" encoding="utf-8"?>
<objects xmlns="http://www.springframework.net"
         xmlns:aop="http://www.springframework.net/aop" >

  <!-- Controllers -->
  <object type="MyApp.Controllers.HomeController, MyApp" singleton="false" />

  <!-- Aop -->
  <object id="myInterceptor" type="Spring.Aop.Support.AttributeMatchMethodPointcutAdvisor, Spring.Aop">
    <property name="Attribute" value="MyApp.Controllers.SetMethodInfoAsMessageAttribute, MyApp" />
    <property name="Advice">
      <object type="MyApp.Controllers.SetMethodInfoAsMessageAdvice, MyApp" />
    </property>
  </object>

  <object type="Spring.Aop.Framework.AutoProxy.InheritanceBasedAopConfigurer, Spring.Aop">
    <property name="ObjectNames">
      <list>
        <value>*Controller*</value>
      </list>
    </property>
    <property name="InterceptorNames">
      <list>
        <value>myInterceptor</value>
      </list>
    </property>
  </object>
</objects>

1 个答案:

答案 0 :(得分:2)

找到它。显然,ProxyDeclaredMembersOnly中有一个属性调用InheritanceBasedAopConfigurer可以启用此行为。最终的Config看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<objects xmlns="http://www.springframework.net"
         xmlns:aop="http://www.springframework.net/aop" >

  <!-- Controllers -->
  <object type="MyApp.Controllers.HomeController, MyApp" singleton="false" />

  <!-- Aop -->
  <object id="myInterceptor" type="Spring.Aop.Support.AttributeMatchMethodPointcutAdvisor, Spring.Aop">
    <property name="Attribute" value="MyApp.Controllers.SetMethodInfoAsMessageAttribute, MyApp" />
    <property name="Advice">
      <object type="MyApp.Controllers.SetMethodInfoAsMessageAdvice, MyApp" />
    </property>
  </object>

  <object type="Spring.Aop.Framework.AutoProxy.InheritanceBasedAopConfigurer, Spring.Aop">
    <property name="ProxyDeclaredMembersOnly" value="false" />
    <property name="ObjectNames">
      <list>
        <value>*Controller*</value>
      </list>
    </property>
    <property name="InterceptorNames">
      <list>
        <value>myInterceptor</value>
      </list>
    </property>
  </object>
</objects>