'Simply about Spring AOP' post illustration

Simply about Spring AOP

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<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 for complimenting main functionality of our program. Like an example let's describe a simple
Config.XML file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;

    xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;

    xmlns:aop=&quot;http://www.springframework.org/schema/aop&quot; >

  <bean id=&quot;audience&quot;                                

      class=&quot;com.springinaction.springidol.Audience&quot; />

  <aop:config>

    <aop:aspect ref=&quot;someadviser&quot;>

      <aop:before

          method=&quot;toDoBefore&quot;

          pointcut=&quot;execution(* *.methodWhereWeNeedToApply (..))&quot; />

      <aop:after-returning

          method=&quot;toDoAfterReturning&quot;

          pointcut=&quot;execution(* *.methodWhereWeNeedToApply(..))&quot; />

      <aop:after-throwing

          method=&quot;toDoAfterThrowing&quot;

          pointcut=&quot;execution(* *.methodWhereWeNeedToApply(..))&quot; />

    </aop:aspect>

  </aop:config>

</beans>

So there are basic information about Spring AOP, but it contains much more. Next week I'll continue our conversation about AOP. I'll tell about AspectJ integration with Spring.

If you're looking for a developer or considering starting a new project,
we are always ready to help!