AOP(Aspect Oriented Programing) is not a technology or a product, its a programming paradigm. Its one of the principles of DRY(Don't Repeat Yourself). The repeating thing here is the aspect or crosscutting concerns. In AOP, these aspects are extracted and then applied to target in a manageable and reusable way.

Some typical scenarios to apply AOP

Database transaction management

For each method that access database which contains transactional data, managing the transaction commit and rollback is a necessary routine procedure but not directly relate to the business.

The typical code of transacation management include start a database session, perform data accessing, if something failed, rollback, otherwise, commit then close session.

Security check

If system defined some resources or methods need necessary authorities to access. The scecurity check must be done before the resource is served.

Mix these kind of code with your business logic is not a good idea.

Logging

Not all loggings are cross cutting concerns. The loggings that specific to the business itself should stay with business code. But the system wide loggings for example, the method calling as a system event that should be logged is cross cutting concerns.

Preprocessing and Postprocessing

For a typical HTTP request, a method need to extract and transform the request parameter to programming data structure. The prameter usually send as string, but the program may need number, object.

At the end of the request handler, the method will try to grab a view template and load it with data. A postprocessing phrase may resolve the view name to one of more view files. You can set the resolving rule and using a simplified name in code to locate view files. An example is Spring view resolver.

AOP in real world

The AOP paradigm has been used widely in various kinds of frameworks and software, especially in WEB related applications because each WEB request handler shares many cross cutting concerns.

Generally there are two forms of AOP: AOP framework and AOP specific to particular application. AOP framework let you customize every aspect of you architecture. In the second one, the system already implemented the core of AOP infrastructure, you only provide you own advice to plug into the system.

Spring AOP framework

May be the most widely used AOP framework, implemented with proxy and intercept. You can define every aspect of AOP, from cross cutting concerns, pointcut, advice. You control everything. Here is an example. Spring JdkRegexpMethodPointcut pointcut example

Servlet filters

All Java EE application starts with a Servlet filter. Actually, you can think of the filter pattern as a kind of AOP. These filters intecept HTTP request and response and do some processing.

By defining URL mapping rules, you can intercept any URL pattern and do preprocessing and postprocessing.

This is AOP specific to Servlet and HTTP request. What you do is implement a predefined interface and set mapping rules.

A typical filter.

 
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
 
import java.io.IOException;
import javax.servlet.ServletException;
 
public class SimpleFilter implements Filter
{
  public void doFilter (ServletRequest request,
             ServletResponse response,
             FilterChain chain) throws IOException, ServletException
  {
 
    // preprocessing
 
 
    chain.doFilter (request, response);
 
    // postprocessing
  }
 
}
 
 

Struts2 interceptor

The Struts2 architecture is heavily based on interceptor. The interceptor is just like an advice, but specific to Struts actions.

The cross cutting functions can be added around the action method's invocation. This largely avoided the maintaining headache.

Interceptors are used for validation, page setting up, session accessing, parameter processing and other system wide functionalities that cuts across many actions.

You see the biggest improvement of Struts framework is introducing AOP approach.

Conclusion

AOP is a very useful and powerful paradigm in software architecture design . We will see more and more frameworks and system are incorporating AOP approach in differernt ways. Its crucial to understand the principles behind AOP, no matter design you own architecture or utilize other frameworks.