Spring.Net Before Advice 不起作用
我正在尝试使用 Spring.Net 实现一个非常基本的 before 建议,它只是将一些信息打印到控制台。这是 spring 配置的相关部分:
<!-- Before Advice: Method Logging -->
<object id="methodLoggingBeforeAdvice"
type="Ch.Test.AddressBook.Common.Advice.MethodLoggingBeforeAdvice" />
<!-- Program Proxy w/ Advice Applied -->
<object id="programProxy"
type="Spring.Aop.Framework.ProxyFactoryObject">
<property name="target" ref="programProxyTarget" />
<property name="interceptorNames">
<list>
<value>methodLoggingBeforeAdvice</value>
</list>
</property>
</object>
<!-- Target, which the advice is applied to -->
<object id="programProxyTarget"
type="Ch.Test.AddressBook.Ui.ConsoleUi.Program">
<constructor-arg ref="repository"/>
</object>
这是建议:
public class MethodLoggingBeforeAdvice : IMethodBeforeAdvice
{
public void Before(MethodInfo method, Object[] args, Object target)
{
// Log method start
Console.Out.WriteLine(
"MethodLoggingBeforeAdvice: Entering method '"
+ method.Name + "'");
// Log method arguments
for (int i = 0; i < args.Length; i++)
{
Console.Out.WriteLine("MethodLoggingBeforeAdvice: Argument " + (i + 1)
+ " - " + args[0].ToString());
}
}
}
一切都构建良好,应用程序正在运行,Program 类被实例化并调用方法,但建议没有输出。我似乎不明白为什么......提前致谢!
I am trying to implement a very basic before advice with Spring.Net, that just prints some information to the console. Here is the relevant part of the spring config:
<!-- Before Advice: Method Logging -->
<object id="methodLoggingBeforeAdvice"
type="Ch.Test.AddressBook.Common.Advice.MethodLoggingBeforeAdvice" />
<!-- Program Proxy w/ Advice Applied -->
<object id="programProxy"
type="Spring.Aop.Framework.ProxyFactoryObject">
<property name="target" ref="programProxyTarget" />
<property name="interceptorNames">
<list>
<value>methodLoggingBeforeAdvice</value>
</list>
</property>
</object>
<!-- Target, which the advice is applied to -->
<object id="programProxyTarget"
type="Ch.Test.AddressBook.Ui.ConsoleUi.Program">
<constructor-arg ref="repository"/>
</object>
Here is the advice:
public class MethodLoggingBeforeAdvice : IMethodBeforeAdvice
{
public void Before(MethodInfo method, Object[] args, Object target)
{
// Log method start
Console.Out.WriteLine(
"MethodLoggingBeforeAdvice: Entering method '"
+ method.Name + "'");
// Log method arguments
for (int i = 0; i < args.Length; i++)
{
Console.Out.WriteLine("MethodLoggingBeforeAdvice: Argument " + (i + 1)
+ " - " + args[0].ToString());
}
}
}
Everything builds fine, the application is running, the Program class gets instantiated and methods are called, but there is not output from the advice. I can't seem to figure out why... Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我觉得自己很愚蠢。
我发布的代码没问题。问题是对象的初始化。这就是我使用的:
我实际上应该获取使用建议的programProxy-Object:
I feel stupid.
The code I posted was alright. The problem was the initializing of the object. This is what I used:
I should actually have fetched the programProxy-Object that uses the advice instead:
您还可以使用匿名内部对象,这样您就不会意外获得不建议的对象:
You could also use an anonymous inner-object so you can't accidentally get an unadvised object: