Java ProgrammingLearn How To Work With Spring AOP Applications

Learn How To Work With Spring AOP Applications

Aspect Oriented Programming (AOP) is another approach to programming like Object Oriented Programming (OOP). However, AOP can complement OOP by providing another way to think about the program structure with which a software application is developed. There are certain similarities in how OOP and AOP are used to build software code although they are named differently – in OOP the unit of modularity is known as class while that in AOP is known as Aspect. AOP applications are developed based on the Spring framework, though the Spring framework can also be used without the AOP. This article discusses how to work with Spring AOP applications.

What is AOP?
AOP, as the name suggests, is another approach to software programming. Let us understand AOP with the help of an example. A program structure for a software application contains three layers: UI layer, business logic layer and database layer. To function properly, all layers need support and communication. Support and communication can be provided by efficient logging, security, transaction management, profiling and more. In AOP parlance, the various units that provide support are known as concerns. Since these concerns provide support across layers, they are also referred to as cross-cutting concerns. These cross-cutting concerns are encapsulated in what are known as Aspects. For example, a logging module can be called an AOP Aspect for logging. Aspects are user-defined and can be defined on run time basis. There are many advantages of using Aspects. One, they are reusable across software applications and enterprises; two, since they contain procedures or functions, software developers just need to use them in their program structure; three, Aspects reduce development overhead.

AOP Concepts
AOP concepts drive the AOP structure and to use AOP, it is required to learn the concepts. The main concepts are explained below.

Concept name

Description

Aspect

Aspect is a module that comprises a set of APIs providing one or more cross-cutting concerns. For example, if the security needs to be implemented in a software application, the security Aspect can provide security concerns across application layers.

Join Point

This is the place in the software application where an action or step will be taken using the Spring AOP framework.

Advice

This is the action to be executed before or after the method execution. Actually, it is the piece of code invoked during the program execution. There are five types of advices: before, after, after-returning, after-throwing and around. For example, <!– a before advice definition –>

<aop:before pointcut-ref=”newbusinessService”

method=”doRequiredTask”/> means that the advice before will be run before the code is executed.

Pointcut

Place in the code where an advice should be executed.

Introduction

Allows you to add classes or methods to existing classes.

Target Object

This is the object that is advised by one or more aspects.

Weaving

It is the process of linking aspects with application objects or types to create an advised object. This can be done at run time, loading time or at the time of compilation.

Building an application with AOP
Now that we have some knowledge of the basics of AOP, we will see below how the concepts can be used to build a code with one or more examples: –

Declare an aspect: –
The example below shows how an aspect can be declared: –

<aop:config>
   <aop:aspect id="aopAspect" ref="aopBean">
   ...
   </aop:aspect>
</aop:config>
<bean id="aopBean" class="...">
...
</bean>

Declare a pointcut: –
The example of pointcut is given below: –

<aop:config>
   <aop:aspect id="aopAspect" ref="aopBean">

   <aop:pointcut id="aopbusinessService"
      expression="execution(* com.eduonix.Emp.getSal(..))"/>
   ...
   </aop:aspect>
</aop:config>
<bean id=”yBean" class="...">
...
</bean>

In the above declaration, a pointcut named aopbusinessService is declared and it will match the execution of getSal() method.

Declare Advice: –
The example below shows how all five advices can declared: –

<aop:config>
   <aop:aspect id="aopAspect" ref="aopBean">
      <aop:pointcut id="aopbusinessService"
         expression="execution(* com.eduonix.aopapp.service.*.*(..))"/>

      <!-- a before advice definition -->
      <aop:before pointcut-ref="aopbusinessService" 
         method="doAOPTask"/>

      <!-- an after advice definition -->
      <aop:after pointcut-ref="aopbusinessService" 
         method="doAOPTask"/>

      <!-- an after-returning advice definition -->
      <!--The doAOPTask method must have parameter named aopVal -->
      <aop:after-returning pointcut-ref="aopbusinessService"
         returning="aopVal"
         method="doAOPTask"/>
      <!-- an after-throwing advice definition -->
      <!--The doAOPTask method must have parameter named aopex -->
      <aop:after-throwing pointcut-ref="aopbusinessService"
         throwing="ex"
         method="doAOPTask"/>
      <!-- an around advice definition -->
      <aop:around pointcut-ref="aopbusinessService" 
         method="doAOPTask"/>
   ...
   </aop:aspect>
</aop:config>

<bean id="yBean" class="...">
...
</bean>

As can be seen from the above example, all five advices were declared using the element.
When this code is executed, it will display the point cuts and the output at those points. The output will depend on the point cut definitions and the advice types.

Conclusion: –
While AOP seems to be a pretty good option because of the reusability aspect of its concerns, it is not without its problems. One, it can be an issue debugging an AOP framework-based code because the business classes are advised only after the aspects are configured. Two, Advise can be given only for public methods and not for methods with default, private, or public visibility. Still, AOP can be a good option because it complements OOP so well.

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -