Spring AOP tutorial

In this Spring AOP tutorial, learn what is aspect-oriented programming with example. Also learn what is advice, join-point, and point-cut expressions and how to use them in Spring application with examples.

When you are developing an enterprise Application, you need the following services commonly.

  1. Low-level services.
  2. Middle-level services
  3. High-level services

Low-level services:
Some of the low-level services are IO. Threading, Networking, etc. Which will be supplied by servers freely.

Middle-level services:
Some of the middle-level services are transaction, security, logging, messaging, etc which has to be implemented by you.
Normally, when you implement business operation you need to write the code for business logic and middle-level services.

Consider the following requirement

Class Account Service
{
public void deposit (...) {
if(x.isCallerInRole(“Teller”)
{
Try{ 
Log.info(....);
Tx.begin();
Op1;
Op2
Tx.commit();
Log.info(...);
}
catch(Exception e)
{
Tx.rollback();
Log.info();
}
}
else { 
Throw some security Exception;
}
}
}

In the above example code.

  • Core business logic is mixed with middle-level services like transaction, security, and logging.
  • When you want to change the existing transactions API or security API or logging API with a new one then you need to re-modify the entire application.
  • This may give the maintenance problem.

Spring AOP – Introduction

  • AOP stands for Aspect-oriented programming.
  • AOP is a new kind of programming technique which is mainly used to separate the commonly required middle-level services logic from the core business logic of the application.
  • The transaction, security, logging, Messaging, etc are the middle-level services which are also called as a cross-cutting concern.

With AOP

Business Services Middle Level Services
Class Account service {
public void deposit (..)
{
OP1;
OP2;
OP3;
}
….
}
class CustomerService {
public void and customer (..) {
OP1;
OP2;
}
..
}
Class TxService {
void begin() {..}
void commit() {… }
}
class Security service {
public void verify user() {

}
}
class LogService {
void Log(..) {
..
}
}

In the above code

  1. core business logic is compeltely seperated from middle level services.
  2. Now when you want to change the existing transaction, security or logging implementations with a new one then that will not impact the business service

Following are various AOP frameworks available

  1. Spring AOP
  2. AspectJ
  3. JBOSS AOP

Spring 2.0 AOP is integrated with AspectJ.
Spring AOP Terminology

  1. Aspect
  2. Advice
  3. JointPoint
  4. Pointcut
  5. Advisor
  6. Target
  7. Proxy
  8. Weaving

Aspect

  • Commonly required middle-level services which you are implementing for your enterprise application are called as Aspects
  • Security, Transactions, Logging, etc are aspects.

Advice

  • implementation of a middle-level service is called as advice
  • implementation of an aspect is called as advice.
  • For Example; Advice is class which contains code for aspects like security, Txs, logging, etc.class TxService(…)
    class SecurityService(…)
    class LogService(..)

Joint Point

  • Joint point is a point in the program execution where you want to apply the advice.
  • Joint point is a point in the program execution where you want to run middle-level services code.

Try {
txs.begin(); before business operations
txs.deposit(); Business operation
txs.commit(); After business opeartion returns the control successfully
}catch(Exception e)
{
txs.rollback(); After business operation throws exception.
}

Spring AOP supports the following Joinpoints

  1. MethodBefore Before invoking method
  2. MethodReturning When a method returns the control successfully
  3. MethodThrowing When a method throws an Exception
  4. MethodAfter When a method returns control anyway.

Pointcut

  • Collection of joinpoint is called as pointCut.
  • By default, advice will be applied for all the business operations of all the business services.
  • When you want to apply the advice for some specified business operations of specified business services then you must define pointcut with the required Aspect() expression.

Advisor
The advisor is a combination of Advice and point cut.

Defining the Advisors: To specify the required Advisors, you nedd to configure the bean in the spring configuration document with default Pointcut Advisor class as follows:

<bean name=”MBA advisor” class=”org.springframework.aop.support.DefaultPointcutAdvisor”>
<property name=”advice” ref=”mba”/>
<property name=”pointcut” ref=”MyPC1”/>
<bean>

Now you have to refer advisor in the proxy factory bean instead of advice.

<bean id=”baseProxy” class=”org.springframework.aop.framework.proxyFactoryBean” abstract=”true”>
<property name=”interceptorName”>
<list>
<value>MBAdvisor</value>
</list>
</property>
<bean>

Note:

  • InterceptorNames are list of advices or advisors.
  • When you use a list of advice then advice will be applied to all the methods of all the business services.
  • When you use a list of advisors then advice will be applied to only for the methods which are matching with the given pointcut expression.

Target
A target is an object of your business service before applying the Advice or advisors.

Proxy
A proxy is an Object of your business service before applying the Advice or advisors.

Weaving
Weaving is the process of applying the advice or advice or advisors to the target objects at given pointcuts to get the proxy objects.

Conclusion:

This article is about Spring AOP. It is a new kind of programming technique which is mainly used to separate the commonly required middle-level services logic from the core business logic of the application. We also learn about spring terminologies.
Please feel free to drop a comment in the below comment box, if you have doubt about the topic or you want to share more information about the topic. Happy Learning!