Pregunta

Estoy tratando de usar Spring.NET AOP en mi proyecto para fines de registro, y dos consejos funcionan muy bien, sin embargo, el tercero no se usa en absoluto.

Aquí está mi cableado:

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

Luego obtengo la instancia GraphicsContextManager de 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

Las partes importantes del ObjectManager que uso para obtener objetos de Spring:

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

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

Spring no arroja ninguna excepción, pero el AfterAdvice también se ignora. Alguna idea de por qué? Otro consejo que creé funcionó sin ningún problema.



_____________ EDITAR: ______________

Agregué una sobrecarga a mi ObjectManager:

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

Si, por lo tanto, uso

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

Transmitir la instancia devuelta por Spring no a su tipo concreto sino a una interfaz, funciona como se esperaba y mi consejo se usa (¡sí!).

¿Pero no entiendo por qué?

¿Fue útil?

Solución

Spring.NET AOP utiliza proxys dinámicos:

  • Si su clase objetivo implementa una interfaz, el proxy implementará la interfaz y delegará llamadas al objeto objetivo
  • Si su clase de destino no implementa una interfaz, el proxy anulará la clase de destino, por lo que debe marcar como virtual su método para ser anulado por el proxy.

Mecanismos de representación: http://www.springframework.net/ doc-latest / reference / html / aop.html # aop-proxy-mecanismo

HTH, Bruno

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top