Unobtrusive AOP with Spring.Net

I am trying to add logging to methods decorated with an attribute using Spring.Net for AOP .

Step 1: Link 'Spring.Core', 'Spring.Aop', 'Common.Logging'

Step 2: Create a Tip:

using AopAlliance.Intercept;

namespace MyApp.Aspects
{
    public class LoggingAdvice : IMethodInterceptor
    {
      public object Invoke(IMethodInvocation invocation)
      {
        //todo: log started
        object rval = invocation.Proceed();
        return rval;
        //todo: log finished
      }
    }
}

Step 3: Create an Attribute:

using System;

namespace MyApp.Aspects
{
  public class LoggingAttribute : Attribute
  {
  }
}

Step 4: Editing web.config

<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.springfrmework.net">

      <object id="loggingAdvice" type="MyApp.Aspects.LoggingAdvice, MyApp"></object>
      <object id="loggingAdvisor" type="Spring.Aop.Support.DefaultPointcutAdvisor, Spring.Aop">
        <property name="Advice" ref="loggingAdvice" />
      </object>

      <object type="Spring.Aop.Framework.AutoProxy.AttributeAutoProxyCreator, Spring.Aop">
        <property name="AttributeTypes" value="MyApp.Aspects.LoggingAttribute"/>
        <property name="InterceptorNames" value="loggingAdvisor"/>
      </object>

    </objects>
  </spring>
</configuration>

Step 5: Decorate the method with the attribute:

using System.Web.Mvc;

namespace MyApp.Controllers
{
  public class MyController : Controller
  {
    [Logging]
    public ActionResult DoStuff()
    {
      //todo: implement
    }
  }
}

advice never starts . What am I missing?

+3
source share
2 answers

, , DoStuff(). , , - , DoStuff() Spring.Net AOP.

Thbsen, spring, . , spring mvc , , .

MVC 3

. .

  • InheritanceBasedAopConfigurer
  • ,

Spring ...

MVC, URL- MVC. Execute(), , , . , .

Spring.NET aop . , aop. - . interfaces ( proxy-target-type="true"). , - spring, . mpc mvc .

... InheritanceBasedAopConfigurer

, InheritanceBasedAopConfigurer. -, , , , , .

, .

xml:

<!-- 
When not specifying an object id or name, 
spring will assign a name to it like [typename]#[0,1,2,..]  
-->  
<object type="MyApp.Controllers.HomeController, MyApp" 
        singleton="false" />

<object id="myInterceptor" type="Spring.Aop.Support.AttributeMatchMethodPointcutAdvisor, Spring.Aop">
  <property name="Attribute" value="MyApp.MyAttribute, MyApp" />
  <property name="Advice">
    <object type="MyApp.MyAdvice, 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>

github. mvc 3 Spring.Net Mvc3 . :

+7

, , , , MyController. , spring MyController. spring , therfore (, , ).

, , , .

, debug-logging spring . spring , , .

, : , : <property name="AttributeTypes" value="MyApp.Aspects.LoggingAttribute"/> , : <property name="AttributeTypes" value="MyApp.Aspects.LoggingAttribute, MyApp"/> spring .

+3

Source: https://habr.com/ru/post/1776955/


All Articles