Spring.net “around advice” interceptor logging using DefaultAdvisorAutoProxyCreator

Spring.net “around advice” interceptor logging using DefaultAdvisorAutoProxyCreator

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.

Share Your Thoughts!

Testimonials

What My Clients Say

Karin - Hope and Future

Brent has always been very prompt and generally exceeds our expectations with his work. His maintenance fee is very reasonable. I strongly recommend him!  We get many compliments on our beautiful web site!

Dan

I contacted Krueger Web Design and Brent did everything.  He setup a test site with the changes so we could verify everything was correct before we transferred from my old hosting provider.  During the setup and conversion Brent was extremely professional and responsive. He responded to any issues the same day.

Dan Kaufman
Emily - GFCLT

We've had a great experience working with Krueger Web Design. Prompt responses, great ideas, effective work... all at a reasonable price! This was the perfect fit for our organization.

Mahmud - ACMC

I would love to recommend you to anyone! You have been very helpful when we needed you the most.

Brian - SEMNIC

Working with Krueger Web Design was effortless. Communicating what I wanted for the website and the results that I saw were identical. The customer service was always prompt and I am completely satisfied with the product.

Zach - Stadium Club

Exactly what I wanted! Fast turnaround time and very affordable. Answered all of our questions quickly and made adjustments promptly. Very easy to work with and puts everything into terminology that even I can understand!

Contact Krueger Web Design

Have a question for me? Need an estimate on some work? You can email me at brent@kruegerwebdesign.com or drop me a line using the button below:

Copyright © 2017 Krueger Web Design - All rights reserved
Web Design in Madison, WI