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