Domanda

Sto cercando di utilizzare Spring.NET AOP nel mio progetto a scopo di registrazione e due consigli funzionano alla grande, tuttavia il terzo non si abitua affatto.

Ecco il mio cablaggio:

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

Ricevo quindi l'istanza GraphicsContextManager da 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

Le parti importanti dell'ObjectManager che uso per ottenere oggetti da Spring:

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

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

Spring non fa eccezione, ma anche AfterAdvice viene ignorato. Qualche idea sul perché? Altri consigli che ho creato hanno funzionato senza problemi.


_____________ MODIFICA: ______________

Ho aggiunto un sovraccarico al mio ObjectManager:

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

Se quindi utilizzo

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

Casting l'istanza restituita da Spring non al suo tipo concreto ma piuttosto a un'interfaccia, funziona come previsto e il mio consiglio viene utilizzato (yay!).

Ma non capisco perché?

È stato utile?

Soluzione

Spring.NET AOP utilizza proxy dinamici:

  • Se la classe target implementa un'interfaccia, il proxy implementerà l'interfaccia e delegherà le chiamate all'oggetto target
  • Se la classe target non implementa un'interfaccia, il proxy sovrascriverà la classe target, quindi è necessario contrassegnare come virtuale il proprio metodo per essere sovrascritto dal proxy.

Meccanismi di proxy: http://www.springframework.net/ doc-latest / riferimento / html / aop.html # AOP-proxy-meccanismo

HTH, Bruno

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top