tags:

views:

127

answers:

1

I am trying to use Spring.NET AOP in my project for logging purpose, and two advices work great, however, the third does not get used at all.

Here is my wiring:

<!-- the "After" Advice -->
<object id="GraphicsContextManagerAfter" type="PicturetoolWeb.App.Advice.GraphicsContextManagerAfter, PicturetoolWeb.App">
</object>

<!-- The Proxy -->
<object id="PicturetoolWeb.ImageLib.Context.GraphicsContextManager" type="Spring.Aop.Framework.ProxyFactoryObject, Spring.Aop">
<property name="target" ref="GraphicsContextManagerTarget"/>
<property name="interceptorNames">
    <list>
        <value>GraphicsContextManagerAfter</value>
    </list>    
</property>  
</object>

I then get the GraphicsContextManager instance from Spring:

var manager = ObjectManager.GetNew<GraphicsContextManager>();
// manager IS a proxy and has the advice set!

var x = manager.DoSomeStuff(); 
// the DoSomeStuff Method is invoked, but my After advice is ignored

The important parts of the ObjectManager I use to get objects from Spring:

    static ObjectManager()
    {
        Context = ContextRegistry.GetContext();
    }

    public static T GetNew<T>()
    {
        return (T)Context.GetObject(typeof(T).FullName);
    }

Spring throws no exception, but the AfterAdvice is ignored, too. Any ideas why? Other advice I created did work without any problems.



___________ EDIT: ____________

I added an overload to my ObjectManager:

    public static T GetNew<T>(string typeFullName)
    {
        var ctx = GetContext();
        return (T)ctx.GetObject(typeFullName);
    }

If I therefore use

var contextManager = ObjectManager.GetNew<IGraphicsContextManager>("GraphicsContextManager");

Casting the instance returned by Spring not to its concrete type but rather to an interface, it works as expected and my advice gets used (yay!).

But I dont understand why?

A: 

Hi,

Spring.NET AOP uses dynamic proxies :

  • If your target class implements an interface, the proxy will implement the interface and delegate calls to the target object
  • If your target class does not implement an interface, the proxy will override the target class so you need to mark as virtual your method to be overrided by the proxy.

Proxying mechanisms : http://www.springframework.net/doc-latest/reference/html/aop.html#aop-proxy-mechanism

HTH, Bruno