Spring.NET不使用AOP AfterReturningAdvice
我正在尝试在我的项目中使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Spring.NET AOP 使用动态代理:
代理机制:
http://www.springframework.net/ doc-latest/reference/html/aop.html#aop-proxy-mechanism
HTH,
布鲁诺
Spring.NET AOP uses dynamic proxies :
Proxying mechanisms :
http://www.springframework.net/doc-latest/reference/html/aop.html#aop-proxy-mechanism
HTH,
Bruno