This week, I would like to talk about aspect-oriented programming. A lot of programming paradigms, like OOP, modular programming, and others, allow for dividing 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 is really needed.
First, let’s recall the example from the last article — a simple calculator consisting of several beans for calculating and saving results. These beans were wired by an XML file. In this way, users can choose the correct beans for corresponding operations (for example, saving data to a database or file).
But there is one important problem. If the user wants to add logging, a huge amount of unnecessary code will be added instead of adding several beans in the right place for logging in our project.
The best solution here is to apply AOP. This technology can be described in terms of advice, pointcuts, and join points — the basic definitions in AOP. So let’s describe them in detail.
Advice defines the specific action we need to perform in our application. Strictly speaking, advice determines both what that action is and when it is executed. To answer “what,” advice specifies the functionality; to answer “where,” aspects define the exact execution points—before or after methods, or when exceptions occur.
Joinpoint is a point in the execution of an application where an aspect can be plugged in. It defines places where advice will be applied in code or opportunities to apply advice.
A pointcut defines where advice will be applied to program execution. The pointcut definition matches one or more joinpoints at which advice should be executed.
For the declaration of these required instances in Spring AOP, the user has to put several simple elements into the XML file.
<aop:advisor> Defines advisor.
<aop:after> Defines applying action after advice.
<aop:after-returning> Defines after-returning advice.
<aop:after-throwing> Defines after-throwing advice.
<aop:around> Defines around advice.
<aop:aspect> Defines an aspect.
<aop:before> Defines before advice.
<aop:config> The top-level AOP element. Most <aop:*> elements must
be contained within <aop:config>.These elements allow building nice structures to complement the main functionality of our program. For example, let’s describe a simple Config.XML file.
<beans xmlns="https://www.springframework.org/schema/beans"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="https://www.springframework.org/schema/aop" >
<bean id="audience"
class="com.springinaction.springidol.Audience" />
<aop:config>
<aop:aspect ref="someadviser">
<aop:before
method="toDoBefore"
pointcut="execution(* *.methodWhereWeNeedToApply (..))" />
<aop:after-returning
method="toDoAfterReturning"
pointcut="execution(* *.methodWhereWeNeedToApply(..))" />
<aop:after-throwing
method="toDoAfterThrowing"
pointcut="execution(* *.methodWhereWeNeedToApply(..))" />
</aop:aspect>
</aop:config>
</beans>So there is basic information about Spring AOP, but it contains much more. Next week I’ll continue our conversation about AOP. I’ll tell you about AspectJ integration with Spring.
