Spring.NET不使用AOP AfterReturningAdvice

发布于 2024-08-04 06:29:53 字数 1835 浏览 7 评论 0原文

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

但我不明白为什么?

I am trying to use Spring.NET AOP in my project for logging purpose, and two advices work great, however, the third does not get used at all.

Here is my wiring:

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

I then get the GraphicsContextManager instance from 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

The important parts of the ObjectManager I use to get objects from Spring:

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

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

Spring throws no exception, but the AfterAdvice is ignored, too. Any ideas why?
Other advice I created did work without any problems.

_____________ EDIT: ______________

I added an overload to my ObjectManager:

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

If I therefore use

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

Casting the instance returned by Spring not to its concrete type but rather to an interface, it works as expected and my advice gets used (yay!).

But I dont understand why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

池予 2024-08-11 06:29:53

Spring.NET AOP 使用动态代理:

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

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

HTH,
布鲁诺

Spring.NET AOP uses dynamic proxies :

  • If your target class implements an interface, the proxy will implement the interface and delegate calls to the target object
  • If your target class does not implement an interface, the proxy will override the target class so you need to mark as virtual your method to be overrided by the proxy.

Proxying mechanisms :
http://www.springframework.net/doc-latest/reference/html/aop.html#aop-proxy-mechanism

HTH,
Bruno

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文