AOP promemoria

We are working hard towards publishing our new service for web-conferencing with augmented reality (project name CalaBoard) and among the programming features of the framework we adopted, Spring, there is Aspect Oriented Programming (AOP).

AOP is a programming technique that allows to apply some side-functionality to a system (e.g. security, transactions, coarse grained logging) without affecting the core business logic at its base. Transactions, one of the examples cited above, could affect various parts of the system: applying transaction logic to each of these parts could be cumbersome as it would require keeping this logic through class-derivation and therefore leading to a complex class hierarchy or, alternatively, to repeat the same code again and again in all classes where it is needed leading to overweight classes and boilerplate code.

This is where AOP comes to rescue: the functionality to be spread across the system is defined through Advices and applied through Pointcuts. In the AOP theory, Advices could be applied both at method invocation level and at property assignment level. Spring allows only method invocation level Advices that can be applied at different moments of the method invocation:

  • Before the method is invoked: the actual invocation of the method can be avoided only by raising an exception. The parameters of the method cannot be replaced but it is possible to operate on them;
  • After the method is invoked: as in the previous case, the actual method invocation can be avoided only by raising an exception. The parameters cannot be replaced but only updated;
  • Around the method invocation: differently from the previous two cases, it is possible to avoid calling the target method at all and its return value can also be replaced;
  • When an exception is thrown: if the target method raises an exception, the Advice intercepts the flow of execution and performs its tasks. The exception cannot be suppressed ;
  • By actually adding new methods to existing classes through Introductions. Introductions are a delicate and sophisticated way of adding functionality to an existing class and they work optimally when the target class implements an interface. It is still possible to use introductions on normal classes but this approach involves the usage of the CGLIB and this affects the way the jar files are deployed.

Pointcuts can be either static or dynamic. Static Pointcuts are applied each time the target method is invoked while dynamic Pointcuts take into consideration the state of the system at runtime and can therefore severely affect performances. One of the predefined static Pointcuts available on Spring is the NameMatchMethodPointcut while the only dynamic one is the ControlFlowPointcut.

In addition to these concepts, the AOP in Spring includes Interceptors, classes that put together Advices and Pointcuts. The ProxyFactoryBean is a FactoryBean (in Spring a class that allows the creation of beans defined in an XML configuration file) that actually creates the proxy classes around the real business-logic classes according to the rules defined in Advices or Advisors.

One final note regards the power of auto-wiring: Spring also provides some special factory-beans that can also automatically associate business-classes to Advisors according to different criteria like the names of the target-classes or the rules defined by the Advisors themselves.

The final thought coming with this promemoria post is about how AOP is going to help us in the CalaBoard project: hopefully this will translate in a better architecture of the system where the distribution of the responsibilities is clear. Starting with such a base, it will be easier to add features and stay in control of the whole.

Explore posts in the same categories: Java, Uncategorized


You can comment below, or link to this permanent URL from your own site.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: