【问题标题】:Spring.NET does not use AOP AfterReturningAdviceSpring.NET 不使用 AOP AfterReturningAdvice
【发布时间】:2009-09-15 15:48:20
【问题描述】:

我试图在我的项目中使用 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 返回的实例不转换为具体类型,而是转换为接口,它按预期工作,并且我的建议被使用(耶!)。

但我不明白为什么?

【问题讨论】:

    标签: c# aop spring.net


    【解决方案1】:

    Spring.NET AOP 使用动态代理:

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

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

    HTH, 布鲁诺

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多