Spring.NET AOP Cheat Sheet

by timvasil 3/31/2008 2:17:00 PM

Getting aspect oriented programming (AOP) working with objects in Spring.NET using the IoC container's XML configuration takes some concentration, since there's plenty going on--from defining the pointcut to coding up the advice to preparing the object factory that wraps the target in a proxy. 

Below is a relatively concise XML configuration to do all of these things:

    <object id="MyPointcutAdvisor" type="Spring.Aop.Support.NameMatchMethodPointcutAdvisor, Spring.Aop">
        <property name="Advice">
            <object type="{ your interceptor type goes here, e.g. something that implements IMethodInterceptor }"/>
        </property>
        <property name="MappedNames">
            <list>
                <value>{ specify a method name to intercept here } </value>
                <value>{ specify a method name to intercept here } </value>
            </list>
        </property>
    </object>
    <object id="MyProxy" type="Spring.Aop.Framework.ProxyFactoryObject, Spring.Aop">
        <property name="Target">
            <object type="{ the wrapped target type goes here }"/>
        </property>
        <property name="InterceptorNames">
            <list>
                <value>MyPointcutAdvisor</value>
            </list>
        </property>
    </object>

Note that you can change NameMatchMethodPointcutAdvisor to something more robust like RegularExpressionMethodPointcutAdvisor if you need to intercept methods based on regular expressions rather than simple names.

To get a reference to the proxy object with advice in place, use ref="MyProxy" in the configuration XML for IoC hookup, or use this code:

MyType proxyObj = (MyType)ContextRegistry.GetContext().GetObject("MyProxy");

You can also wrap a pre-instantiated object at runtime:

MyType proxyObj = (MyType)ContextRegistry.GetContext().ConfigureObject(obj, "MyProxy");

Important note:  Spring.NET proxies use a decorator (inheritance) pattern or a composition pattern.  Neither approach allows advice on methods that are not interface implementations and not virtual!  Further, Spring.NET will not warn you or give you an error if it cannot configure advice for a non-virtual, non-interface method; instead, the methods will simply not be intercepted, leading to sometimes confusing bugs.  This issue is documented on the Spring.NET forums and bug tracker.

The decorator (inheritance) pattern is used when the target being proxied has no interfaces to proxy or ProxyTargetType is set to true on the proxy factory.  In this case the target type is subclassed and contains an internal pointer to the target object.  Otherwise the composition pattern is used, where a new class is defined dynamically implementing all the interfaces and likewise contains an internal pointer to the target object.

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Spring.NET | AOP

Related posts

Comments are closed

 

About the author

Tim Vasil Tim Vasil
I'm a software engineer living in Cambridge, MA.

E-mail me Send mail

Search

Calendar

<<  September 2010  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

View posts in large calendar

Recent comments