Spring.Net Before Advice 不起作用

发布于 2024-09-24 15:00:06 字数 1517 浏览 3 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你另情深 2024-10-01 15:00:06

我觉得自己很愚蠢。

我发布的代码没问题。问题是对象的初始化。这就是我使用的:

var program = (Program)ContextRegistry.GetContext().GetObject("program");
program.StartUp();

我实际上应该获取使用建议的programProxy-Object:

var program = (Program)ContextRegistry.GetContext().GetObject("programProxy");
program.StartUp();

I feel stupid.

The code I posted was alright. The problem was the initializing of the object. This is what I used:

var program = (Program)ContextRegistry.GetContext().GetObject("program");
program.StartUp();

I should actually have fetched the programProxy-Object that uses the advice instead:

var program = (Program)ContextRegistry.GetContext().GetObject("programProxy");
program.StartUp();
皓月长歌 2024-10-01 15:00:06

您还可以使用匿名内部对象,这样您就不会意外获得不建议的对象:

  <!-- Before Advice: Method Logging -->
  <object id="methodLoggingBeforeAdvice"
     type="Ch.Test.AddressBook.Common.Advice.MethodLoggingBeforeAdvice" />

  <!-- Program Proxy w/ Advice Applied using an anonymous inner object-->
  <object id="program"
     type="Spring.Aop.Framework.ProxyFactoryObject">
    <property name="target">
       <object type="Ch.Test.AddressBook.Ui.ConsoleUi.Program">
           <constructor-arg ref="repository"/>
        </object>
    </property>
    <property name="interceptorNames">
      <list>
        <value>methodLoggingBeforeAdvice</value>
      </list>
    </property>
  </object>

You could also use an anonymous inner-object so you can't accidentally get an unadvised object:

  <!-- Before Advice: Method Logging -->
  <object id="methodLoggingBeforeAdvice"
     type="Ch.Test.AddressBook.Common.Advice.MethodLoggingBeforeAdvice" />

  <!-- Program Proxy w/ Advice Applied using an anonymous inner object-->
  <object id="program"
     type="Spring.Aop.Framework.ProxyFactoryObject">
    <property name="target">
       <object type="Ch.Test.AddressBook.Ui.ConsoleUi.Program">
           <constructor-arg ref="repository"/>
        </object>
    </property>
    <property name="interceptorNames">
      <list>
        <value>methodLoggingBeforeAdvice</value>
      </list>
    </property>
  </object>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文