2009-09-14 9 views
1

J'essaie d'utiliser Spring.NET AOP dans mon projet pour la consignation, et deux conseils fonctionnent très bien, cependant, le troisième ne s'utilise pas du tout.Spring.NET n'utilise pas AOP AfterReturningAdvice

Voici mon câblage:

<!-- 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> 

je puis obtenir l'instance GraphicsContextManager du printemps:

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 

Les parties importantes du ObjectManager-je utiliser pour obtenir des objets du printemps:

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

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

Spring ne fait pas exception, mais AfterAdvice est également ignoré. Des idées pourquoi? D'autres conseils que j'ai créés ont fonctionné sans aucun problème.



_____________ EDIT: ______________

J'ai ajouté une surcharge à mon ObjectManager:

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

Si j'utilise donc

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

Jetant l'instance renvoyée par Spring non à son type concret mais plutôt à une interface, cela fonctionne comme prévu et mon conseil est utilisé (yay!).

Mais je ne comprends pas pourquoi?

Répondre

0

Spring.NET AOP utilise des procurations dynamiques:

  • Si votre classe cible implémente une interface, le proxy implémenter l'interface et délégué appelle à l'objet cible
  • Si votre classe cible ne met pas en œuvre une interface, le proxy remplacera la classe cible, vous devez donc marquer comme virtuelle votre méthode à remplacer par le proxy.

mécanismes de mandatement: http://www.springframework.net/doc-latest/reference/html/aop.html#aop-proxy-mechanism

HTH, Bruno