November 26, 2014
Hot Topics:

Aspect-Oriented Programming (AOP) with Spring.Net

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

This example code logs the end of the method and the return value (if one is returned). Also, in the same way that before advice implements a special interface, this code implements the Spring.Aop.IMethodAfterAdvice interface indicating that it is after advice.

For the configuration, after advice is fairly similar to before advice. Just insert the after advice as the interceptor instead of the before advice.

Listing 5: After Advice Configuration Example: Method Logging

<!-- After Advice Example: Method Logging -->
<object id="methodLoggingAfterAdvice"
   type="SpringAOPExample.Aspects.MethodLoggingAfterAdvice" />

<!-- Example Service Proxy w/ Advice Applied -->
<object id="exampleService"
   type="Spring.Aop.Framework.ProxyFactoryObject">
   <property name="target" ref="exampleServiceTarget" />
   <property name="interceptorNames">
      <list>
         <value>methodLoggingAfterAdvice</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>
Throws Advice

The next type of advice you will look at is throws advice. Unlike the other types of advice, throws advice is not run every time a method is called. Instead, it runs only when an exception is thrown in the method. Throws advice is useful whenever you want to set up some special error handling or error logging and apply it across your application.

Listing 6: Throws Advice Code Example: Error Handling

public class ErrorHandlingThrowsAdvice : IThrowsAdvice {

   public void AfterThrowing(ApplicationException ex) {

      // Do some special error handling for an application
      // exception...

      // Log the error
      Console.Error.WriteLine("Method threw an application
                               exception: "
         + ex.Message);
   }

   public void AfterThrowing(Exception ex) {

      // Do some general error handling...

      // Log the error
      Console.Error.WriteLine("Method threw a general
                               exception: "
         + ex.Message);
   }
}

Notice that in this example there are two implementations of the AfterThrowing() method with different exception types. With throws advice, you can specify which type or types of exceptions to handle. In this example, if an ApplicationException is thrown, the first method will run; if some other exception is thrown, the second method will run. Also, if you want, you can remove the second generic Exception method and then this advice will only run if an ApplicationException is thrown and be ignored if any other type of exception is thrown. This is useful if you only want to run special error handling for a certain exception type.

Also, as with other advice, throws advice implements the Spring.Aop.IThrowsAdvice interface in the same manner that the other types of advice implement their own interfaces.

As far as the configuration of throws advice goes, it is pretty much the same deal as before and after advice.

Listing 7: Throws Advice Configuration Example: Error Handling

<!-- Throws Advice Example: Error Handling -->
<object id="errorHandlingThrowsAdvice"
   type="SpringAOPExample.Aspects.ErrorHandlingThrowsAdvice" />

<!-- Example Service Proxy w/ Advice Applied -->
<object id="exampleService"
   type="Spring.Aop.Framework.ProxyFactoryObject">
   <property name="target" ref="exampleServiceTarget" />
   <property name="interceptorNames">
      <list>
         <value>errorHandlingThrowsAdvice</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>
Around Advice

The last and most powerful type of advice you will look at is also the root type of advice on which the other types of advice are built. Around advice does not specify where your advice will be run in relation to a method; instead, it wraps the method execution and allows you to control where around the method execution you want to run your advice code.


Tags: .NET



Page 3 of 6



Comment and Contribute

 


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

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel