我有一个由 Spring.NET 上下文拥有的(原型)类,它是使用带有运行时构造函数参数的 AutoProxy 创建的。它像这样被实例化。
var context = ContextRegistry.GetContext();
var myClass = context.GetObject("myclass", new object[]{ myRuntimeConstructorArgument }) as MyClass;
这个类有一些内部逻辑,它捕获由类方法处理的事件,如下所示:
// MyClass owned by Spring.NET context
public class MyClass {
// InnerObject not owned by Spring context
private InnerObject innerobject;
// argument object not owned by Spring context
public MyClass(ObjectNotOwnedBySpring myRuntimeConstructorArgument) {
...
this.innerobject = new InnerObject();
this.innerobject.SomeEvent += this.handleSomeEvent;
}
// I would like to intercept this event handler
public virtual void handleMyEvent (object sender, EventArgs args)
{
// do stuff
}
}
我尝试使用 AOP 通过拦截 handleMyEvent 方法来消除抽象泄漏(由外部组件中的错误引起)。由于 Spring.Aop 使用基于代理的方法来拦截方法调用,因此内部方法调用不会被拦截。我明白那部分。
如果我理解的话文档 http://www.springframework.net/doc-latest/reference/html/aop.html#aop-proxy-mechanism正确的是,这就是基于继承的AopConfigurer通过使用“真正的”基于继承的 AOP 机制发挥作用。从文档中:
在调用基类方法之前,拦截建议直接添加到方法体中。
正是我需要的!
不幸的是,我无法让 Inheritance BasedAopConfigurer 与运行时构造函数参数很好地配合:
Spring.Objects.Factory.ObjectCreationException:创建名称为“myclass”的对象时出错 > 在“文件 [spring-aop-config.xml] 第 36 行”中定义:无法解析匹配的构造函数。
InheritanceBasedAopConfigurer 不支持运行时构造函数参数吗?或者这是 InheritanceBasedAopConfigurer 的错误用例?