Aspect Oriented Programming : Overview

Mithun Sasidharan
4 min readAug 10, 2016

--

In my previous blog where I had explained the Proxy Design Pattern, I did mention that Spring Aspect Oriented Programming is based on the proxy design pattern principle. In this blog, I will share my understanding about AOP and how can it be leveraged in application to solve cross cutting concerns.

To start with, we first need to understand what cross cutting concerns and what concerns are in general. A concern is a particular set of information that has an effect on the code of a computer program. A concern can be as general as the details of database interaction or as specific as performing a primitive calculation. Usually the code can be separated into logical sections, each addressing separate concerns, and so it hides the need for a given section to know particular information addressed by a different section. Consider the below code snippet.

The function encapsulates a set of different concerns that includes permission validation, logging, transactions and business logic. These concerns together accomplish the purpose of the function. In short, a concern is a particular issue, concept, or area of interest for an application: typically a goal the application must meet.

Now what are cross cutting concerns? Cross cutting concerns are concerns that cut across multiple application modules. These concerns often cannot be cleanly decomposed from the rest of the system in both the design and implementation, and can result in either scattering (code duplication), tangling (significant dependencies between systems), or both. For example, it’s possible that we have permission validation and logging in almost all functions and modules there by making it a cross cutting concerns. Aspect oriented programming help us with the separation of cross cutting concerns and decouple them from the core application modules. The below diagram should help you give a fair understanding of cross cutting concerns.

Aspect-oriented programming, or AOP, is a programming technique that allows programmers to modularize crosscutting concerns. It is often defined as a programming technique that promotes separation of crosscutting concerns with in a software system.

So basically AOP helps you solve cross cutting concerns across application modules by separating them off the other concerns. It’s important to understand some of the key terminologies involved in AOP.

  • Aspect: A modularization of a concern that cuts across multiple classes. Transaction management is a good example of a crosscutting concern in J2EE applications. In Spring AOP, aspects are implemented using regular classes or regular classes annotated with the@Aspectannotation (the @AspectJstyle).
  • Join point: A point during the execution of a program, such as the execution of a method or the handling of an exception. In Spring AOP, a join point alwaysrepresents a method execution.
  • Advice: Action taken by an aspect at a particular joins point. Different types of advice include “around,” “before” and “after” advice.
  • Pointcut: A predicate that matches join points. Advice is associated with a pointcut expression and runs at any join point matched by the pointcut (for example, the execution of a method with a certain name). The concept of join points as matched by pointcut expressions is central to AOP, and Spring uses the AspectJ pointcut expression language by default.
  • Target object: object being advised by one or more aspects. Also referred to as theadvisedobject. Since Spring AOP is implemented using runtime proxies, this object will always be a proxied object.
  • AOP proxy: an object created by the AOP framework in order to implement the aspect contracts (advises method executions and so on). In the Spring Framework, an AOP proxy will be a JDK dynamic proxy or a CGLIB proxy.
  • Weaving: linking aspects with other application types or objects to create an advised object. This can be done at compile time (using the AspectJ compiler, for example), load time, or at runtime. Spring AOP, like other pure Java AOP frameworks, performs weaving at runtime.

The below diagram should give you an idea on how Spring AOP works once the you set a pointcut on a method and load the application.

As you can see when a method on which a pointcut is applied gets invoked, the calls go to the proxy object which executes the advice and subsequently the real method is invoked. This is how the AOP accomplishes separation of concerns.

Aspect oriented programming isn’t an independent style of programming. You can use Aspect Oriented programming in combination with functional programming, but also in combination with Object oriented one.

--

--