March 5, 2021
Hot Topics:

Aspect-Oriented Programming (AOP) with Spring.Net

  • By David Consdorf
  • Send Email »
  • More Articles »


Aspect-Oriented Programming (AOP) allows developers to achieve greater modularity in their applications by allowing the separation of cross-cutting concerns. In other words, with AOP it is easy to separate out the logic for those repetitive and messy tasks such as logging, security, and error handling that span the entire application. In this article, I will discuss basic AOP concepts and their implementation using Spring.Net, a popular open source application framework. I will also walk you through several examples of AOP with Spring.Net.

Aspect-Oriented Programming

Aspect-Oriented Programming (AOP) is defined as a programming paradigm that promotes application modularity through the separation of cross-cutting concerns. To understand what this means, you need to start with understanding the problem it solves.

Object-Oriented Programming (OOP) has been a great innovation in the world of software development. Built on the idea that an application is simply a collection of interacting entities, OOP allows for very modular and easily conceptualized applications. However, OOP is not perfect. Sometimes, there are secondary pieces of logic that do not fit into the actions of any specific entity within an application and instead affect many unrelated parts of the application. These pieces of logic that defy the application model are known as cross-cutting concerns.

Logging is a typical example of a cross-cutting concern. When you need to add or change the basic logging within an application, you generally need to change the logic for multiple entities. This can be messy because it often includes a lot of scattered, repetitive code that requires more effort to develop, test, and maintain. AOP attempts to alleviate this problem by separating out the cross-cutting logic into its own module or modules and applying that logic transparently across the various entities of an application without needing to change the logic for each entity directly.

In AOP terminology, a piece of cross-cutting code you want to apply across the application is known as advice. So, for your logging example, you might have some advice that logs entering and exiting methods. The points in the code where you want the advice to run are known as pointcuts. For your logging example, the pointcuts would be directly before and after the monitored methods are executed. This may be for all methods of an application or only a certain subset that meet some specified criteria. Finally, the combination of advice and pointcut is called an aspect. For your example, youwe would have a method logging aspect. Add a few more aspects to the application and you have aspect-oriented programming.

This is a fairly abbreviated explanation of AOP, but it should be a decent start for the purposes of this article. If you want to learn more and delve into some other AOP concepts, check out http://en.wikipedia.org/wiki/Aspect-oriented_programming and other various resources on the Net.

Aspect-Oriented Programming with Spring.Net

So, where does Spring.Net fit in? Well, Spring.Net is an open source framework that provides a toolset of components that help developers implement industry best practices such as Dependency Injection and Aspect-Oriented Programming.

There are several other worthwhile AOP frameworks out there like PostSharp, AspectDNG, and Aspect.Net. Some even afford greater performance because they implement rather powerful compile-time IL weaving as opposed to runtime weaving like Spring.Net uses. These frameworks, however, do not have the breadth of tools that Spring.Net has.

AOP is not the only purpose for using Spring or even the primary purpose in many cases. Chances are that you have heard of or are already using Spring.Net for your Dependency Injection needs by using Spring.Net's simple yet powerful Inversion of Control (IoC) container. The AOP framework is designed to complement the IoC container and create a very robust overall solution to wiring your application together.

Before you continue, it is important that you understand Dependency Injection with Spring.Net because this article builds on this core concept of Spring.Net. If you need a good introduction or a quick refresher, here is a link to my earlier article on the subject: "Dependency Injection with Spring.Net."

There is also a plethora of documentation and reference links to be found on the Sprint.Net website, www.springframework.net.

Implementing AOP in Spring.Net

Now, you dive into some examples of Spring.Net AOP in action. For this article, you will be working with the following generic ExampleService class and touch on how to create and apply some basic aspects in Spring.Net through the use of advice and pointcuts.

Listing 1: Example Service Class

public interface ExampleService {

   Object getObject(long id);
   void saveObject(Object obj);
   void runOperation(Object obj);
   void runOperationWithError(Object obj);

public class ExampleServiceImpl : ExampleService {

   public Object getObject(long id) {
      // Run some logic to retrive an object
      return "testObject";

   public void saveObject(Object obj) {
      // Run some logic to save an object

   public void runOperation(Object obj) {
      // Run some logic

   public void runOperationWithError(Object obj) {
      // Run some logic and throw an error...
      throw new ApplicationException("This method threw
                                      an error!!!");

Page 1 of 6

This article was originally published on January 8, 2009

Enterprise Development Update

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

Thanks for your registration, follow us on our social networks to keep up-to-date