Question

I am having trouble with Spring.Net's AOP. It seems that interceptors will not fire if the given method is an inherited method from a base class.

However, if I override the inherited method from the base, the interceptors will fire.

Is there a way to make the interceptor fire without overriding the base?

Here's what I have so far: (Modified version of 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
{
}    

With the following 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="ObjectNames">
      <list>
        <value>*Controller*</value>
      </list>
    </property>
    <property name="InterceptorNames">
      <list>
        <value>myInterceptor</value>
      </list>
    </property>
  </object>
</objects>
Was it helpful?

Solution

Found it. Apparently there is a property call ProxyDeclaredMembersOnly in InheritanceBasedAopConfigurer that enables this behavior. The final Config looks like this:

<?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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top