This week I would like to talk about aspect oriented programming. A lot of programming paradigms like OOP, modular programming and other, let to divide functionality into logical parts (functions modules, classes). But these functional elements may be marked out into separate modules. Aspect oriented programming (AOP) has as a target to provide separation of functionality and addition to points where it really needs.
First of all let's remember an example from last article – simple calculator consisting form several beans for calculating and saving results. These beans were wired by XML file. In a such a way user can choose correct beans for applying correspond operations (for example save data to db or to file).
But there is one important problem. If user wants to add logging, huge amounts of needless code will be added instead of adding several beans in right place for logging implementation in our project.
The best solution in this case is applying of AOP. This technology can be described in terms of advices, pointcuts, and joinpoints. There are basic definitions in AOP. So let's describe them in detail.
Advice is some job what we need to do in our application. Strictly saying advice defines what and when some job will be done. Answering the question what is user going to do advice define some functionality or on another hand in aspect we can define exactly where some job will be executed before or after method in our code or when some exception will be thrown.
Joinpoint is some point in execution of application where an aspect can be plugged in. It defines places where advice will be applied in code or opportunities to apply advice.
Pointcut defines where advice will be applied to program execution. The pointcut definition matches one or more joinpoints at which advice should executed.
For declaration of these required instances in Spring AOP user has to put into XML file several simple elements.
These elements allow building nice structures for complimenting main functionality of our program. Like an example let's describe a simple