tags:

views:

19

answers:

1

Hi everybody,

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!

A: 

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();
Christopher