I wanted to be able to wrap all method calls from any of our spring.net created objects for the purpose of some sort of performance logging. This could have been done at least two different ways

  1. Using a Spring.Aop.Framework.AutoProxy.ObjectNameAutoProxyCreator and specifying a wildcard expression to wrap advice around certain objects that match the expression. (all services only, all data access objects, etc). From the Spring.NET AOP documentation: “The ObjectNameAutoProxyCreator automatically creates AOP proxies for object with names matching literal values or wildcards. The pattern matching expressions supported are of the form “*name”, “name*”, and “*name*” and exact name matching, i.e. “name”. The following simple classes are used to demonstrate this autoproxy functionality.”
  2. Using the Spring.Aop.Framework.AutoProxy.DefaultAdvisorAutoProxyCreator to apply advice to ALL spring created objects. From the Spring.NET AOP documentation: “A more general and extremely powerful auto proxy creator is DefaultAdvisorAutoProxyCreator. This will automatically apply eligible advisors in the current application context, without the need to include specific object names in the autoproxy advisor’s object definition. It offers the same merit of consistent configuration and avoidance of duplication as ObjectNameAutoProxyCreator.”

I went with option 2 as it was exactly what I was looking for, but like the documentation said, be forewarned that the DefaultAdvisorAutoProxyCreator is a very powerful tool, and could have serious implications on your application if not used carefully, as it  can touch all spring created objects. So be sure to not try and do too much here.

First you will need to create your interceptor class, in my case I called it ConsoleLoggingAroundAdvice, and it looks like this (very basic):

public class ConsoleLoggingAroundAdvice : IMethodInterceptor
        {
            public object Invoke(IMethodInvocation invocation)
            {
                object returnValue = invocation.Proceed();
                return returnValue;
            }
        }

One thing to note here is that a call to Proceed() must be made to invoke the objects method call. We can simply wrap this call with whatever logic we want. We could create a stopwatch to time the execution length, or do some custom logging, etc.  Anything before Proceed() is the before advice, and after is the after advice, so in conjunction we have “around advice”. From the Spring.NET AOP documentation: “Around advice: Advice that surrounds a joinpoint such as a method invocation. This is the most powerful kind of advice. Around advice will perform custom behaviour before and after the method invocation. They are responsible for choosing whether to proceed to the joinpoint or to shortcut executing by returning their own return value or throwing an exception.”

Now for what was initially the tricky part for me, the Spring.NET configuration of this interceptor, well… it looks like this:

<object type="Spring.Aop.Framework.AutoProxy.DefaultAdvisorAutoProxyCreator, Spring.Aop">
  <property name="InterceptorNames">
    <list>
      <value>ConsoleLoggingAroundAdvice</value>
    </list>
  </property>
</object>

This is basically saying, for all of our Spring.NET created objects, intercept their method calls (Public only I believe…don’t quote me on that :) ) with our ConsoleLoggingAroundAdvice implementation.

This post doesn’t go into a lot of detail, but I wanted to make sure that someone else could hopefully find the information they needed to implement around advice on all Spring.NET objects in their application.

Enjoy.