1

我试图在我的项目中使用 Spring.NET AOP 进行日志记录,两个建议效果很好,但是,第三个根本没有被使用。

这是我的接线:

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

然后我从 Spring 获取 GraphicsContextManager 实例:

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

我用来从 Spring 获取对象的 ObjectManager 的重要部分:

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

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

Spring 不抛出异常,但 AfterAdvice 也被忽略。任何想法为什么?我创建的其他建议确实有效,没有任何问题。



_____________ 编辑: ______________

我向我的 ObjectManager 添加了一个重载:

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

如果我因此使用

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

将 Spring 返回的实例转换为不是其具体类型而是转换为接口,它按预期工作,并且我的建议被使用(耶!)。

但我不明白为什么?

4

1 回答 1

0

Spring.NET AOP 使用动态代理:

  • 如果您的目标类实现了一个接口,代理将实现该接口并将调用委托给目标对象
  • 如果您的目标类未实现接口,则代理将覆盖目标类,因此您需要将要被代理覆盖的方法标记为虚拟。

代理机制: http ://www.springframework.net/doc-latest/reference/html/aop.html#aop-proxy-mechanism

HTH,布鲁诺

于 2009-09-15T07:48:20.197 回答