【发布时间】:2011-11-07 14:13:51
【问题描述】:
我正在学习 Spring.Net 并且正在尝试一些简单的东西,但这是行不通的。我想记录任何用LogCall 装饰的方法调用
namespace WpfApplication1
{
public partial class MainWindow : Window
{
public MainWindow()
{
Test();
InitializeComponent();
}
[LogCall]
public void Test()
{
}
}
public class LogCallInterceptor : IMethodBeforeAdvice
{
public void Before(MethodInfo method, object[] args, object target)
{
Debug.Write(method.Name);
}
}
[Serializable]
[AttributeUsage(AttributeTargets.Method)]
public class LogCallAttribute : Attribute
{
}
}
这是 App.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<spring>
<objects xmlns="http://www.springframework.net">
<object id="TestLogAdvice" type="Spring.Aop.Support.AttributeMatchMethodPointcutAdvisor, Spring.Aop">
<property name="advice">
<object type="WpfApplication1.LogCallInterceptor, WpfApplication1" />
</property>
<property name="attribute" value="WpfApplication1.LogCallAttribute, WpfApplication1" />
</object>
</objects>
</spring>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Spring.Core" publicKeyToken="65e474d141e25e07" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-1.3.2.40943" newVersion="1.3.2.40943" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Spring.Aop" publicKeyToken="65e474d141e25e07" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-1.3.2.40943" newVersion="1.3.2.40943" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
我对这一切真的很陌生,所以我什至不确定这是否是一种有效的方法。
根据第一个答案,我修改了我的示例。还是行不通?我暖和了吗?
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
var someClass = new SomeClass();
someClass.Test();
InitializeComponent();
}
}
public class SomeClass
{
[LogCall]
public void Test()
{
}
}
public class LogCallInterceptor : IMethodBeforeAdvice
{
public void Before(MethodInfo method, object[] args, object target)
{
Debug.Write(method.Name);
}
}
[Serializable]
[AttributeUsage(AttributeTargets.Method)]
public class LogCallAttribute : Attribute
{
}
}
还有新的 app.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<spring>
<objects xmlns="http://www.springframework.net">
<object id="TestLogAdvice" type="Spring.Aop.Support.AttributeMatchMethodPointcutAdvisor, Spring.Aop">
<property name="advice">
<object type="WpfApplication1.LogCallInterceptor, WpfApplication1" />
</property>
<property name="attribute" value="WpfApplication1.LogCallAttribute, WpfApplication1" />
</object>
</objects>
<object id="mySomeClass" type="Spring.Aop.Framework.ProxyFactoryObject">
<property name="target">
<object id="mySomeClassTarget" type="WpfApplication1.SomeClass"/>
</property>
<property name="interceptorNames">
<list>
<value>TestLogAdvice</value>
</list>
</property>
</object>
</spring>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Spring.Core" publicKeyToken="65e474d141e25e07" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-1.3.2.40943" newVersion="1.3.2.40943" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Spring.Aop" publicKeyToken="65e474d141e25e07" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-1.3.2.40943" newVersion="1.3.2.40943" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
【问题讨论】:
-
我已经更新了我的答案,你已经接近了。
-
您的 xml 中有一个错误: 应该被移动。
-
如果这是你完整的 app.config,它也不起作用,我会在我的答案下面写一个注释。
标签: logging aop spring.net