September 30, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Aspect-Oriented Programming (AOP) with Spring.Net

  • January 8, 2009
  • By David Consdorf
  • Send Email »
  • More Articles »

Listing 8: Around Advice Code Example: Performance Logging

public class PerformanceLoggingAroundAdvice :
   IMethodInterceptor {

   public object Invoke(IMethodInvocation invocation) {

      Stopwatch stopwatch = new System.Diagnostics.Stopwatch();

      // Start timing the method
      stopwatch.Start();

      // Execute the method
      Object returnValue = invocation.Proceed();

      // Stop timing the method
      stopwatch.Stop();

      // Log method runtime
      Console.Out.WriteLine("ExampleAroundAdvice: '"
         + invocation.Method.Name + "' took "
         + stopwatch.ElapsedMilliseconds
         + " milliseconds to run.");

      return returnValue;
   }
}

In this example, you use the system stopwatch to time how long it takes to run the method; this gives you a simple performance monitor at the method level.

Note the use of the invocation.Proceed() method to execute the advised method. Around advice gives you control of when and even if the advised method is called. It also gives you control of what to do with the result of the method execution, allowing you to modify or filter the result if needed. This combination affords you great flexibility in setting up your advice.

In fact, with the power of around advice, you could just ignore the other advice types and use around advice to implement before, after, and throws advice. This is not really recommended, though, because the extra power of around advice comes at the cost of requiring you to make sure you call invocation.Proceed() and return the results properly for every piece of advice. You can avoid the extra coding and testing for potential errors by using one of the more specific implementations.

Also, note that instead of implementing a specific Spring.Net interface, around advice implements the AopAlliance.Intercept.IMethodInterceptor interface, which is a standard AOP interface meant to standardize the various AOP implementations. The other types of advice (before, after, and throws) are really just more specific implementations of this general IMethodInterceptor interface.

And again, as far as the configuration of around advice goes, it is pretty much the same deal as the other types of advice.

Listing 9: After Advice Configuration Example: Method Logging

<!-- Around Advice Example: Performance Logging -->
<object id="performanceLoggingAroundAdvice"
   type="SpringAOPExample.Aspects.PerformanceLoggingAroundAdvice" />

<!-- Example Service Proxy w/ Advice Applied -->
<object id="exampleService"
   type="Spring.Aop.Framework.ProxyFactoryObject">
   <property name="target" ref="exampleServiceTarget" />
   <property name="interceptorNames">
      <list>
         <value>performanceLoggingAroundAdvice</value>
      </list>
   </property>
</object>

<object id="exampleServiceTarget"
   type="SpringAOPExample.Service.Impl.ExampleServiceImpl">
   <!-- Note: This is just a standard service object. Inject
        dependencies as needed (ie. DAOs, Other Services). -->
</object>
Pointcuts

So, now you know how to create some advice and apply it to the methods of one or more services. But, what if you don't want to apply the advice to all the service methods? For example, what if your advice is a security check algorithm that only needs to run for protected methods, such as saving a new or updated object?

This is where pointcuts come into the picture. A pointcut allows you to specify criteria for when to execute advice so you can direct advice to only execute for certain methods.

Start by creating some generic security advice to run before secured methods.

Listing 10: Pointcut Code Example: Security Check Before Advice

public class SecurityCheckBeforeAdvice : IMethodBeforeAdvice {

   public void Before(MethodInfo method, Object[] args,
      Object target) {

      // Put your security logic here
      // Throw security exception if there is an error...

      // Log method start
      Console.Out.WriteLine(
         "SecurityCheckBeforeAdvice: Access granted for '"
         + method.Name + "' method");
   }
}

As you can see, this is just a standard piece of before advice. There is nothing special in the advice code to specify any pointcuts. Instead, pointcuts are set up in the configuration.


Tags: .NET



Page 4 of 6



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel