Spring AOP

Spring AOP XML Schema based Example

In this chapter we will see that Implementation of AOP based on XML Schema and also look the relative code in annotation based. Before using the AOP we have to add namespaces of AOP to the configuration file as following.
<beans xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:>
You will also need following AspectJ libraries on the CLASSPATH of your application. These libraries are available in the 'lib' directory of an AspectJ installation, otherwise you can download them from the internet. 1. aspectjrt.jar 2. aspectjweaver.jar 3. aspectj.jar

Declaring an aspect

An aspect is declared using the <aop:aspect> element, and the backing bean is referenced using the ref attribute as follows:
<bean class="com.sdnext.aop.tutorial.aspectJ.LoggingAspect" id="loggingAspect">
  ....
</bean>
<aop:config>
   <aop:aspect id="loggingAspect" ref="loggingAspect">
   ....
   </aop:aspect>
</aop:config>

Here "loggingAspect" will be configured and dependency injected just like any other Spring bean as you have seen in previous chapters.

Declaring a pointcut

A pointcut helps in determining the join points (ie methods) of interest to be executed with different advices. While working with XML Schema based configuration, pointcut will be defined as follows:

<aop:config>
   <aop:aspect id="loggingAspect" ref="loggingAspect">

   <aop:pointcut expression="execution(* com.sdnext.aop.service.*.*(..))" id="businessService"> </aop:pointcut>
   ...
   </aop:aspect>
</aop:config>

<bean class="com.sdnext.aop.tutorial.aspectJ.LoggingAspect" id="loggingAspect">
  ....
</bean>

The following example defines a pointcut named 'businessService' that will match the execution of all getter as like getName() etc method available in Circle class under the package com.sdnext.aop.tutorial:

<aop:config>
   <aop:aspect id="loggingAspect" ref="loggingAspect">

   <aop:pointcut expression="execution(* com.sdnext.aop.tutorial.Circle.getName(..))" id="businessService"></aop:pointcut>
       ....
   </aop:aspect>
</aop:config>

<bean class="com.sdnext.aop.tutorial.aspectJ.LoggingAspect" id="loggingAspect">
  ....
</bean>


Declaring advices

You can declare any of the five advices inside an <aop:aspect>...</aop:aspect> using the <aop: name="name">...</aop:>element as given below:

1.before advice- <aop:before></aop:before >
2.after advice- <aop:after></aop:after>
3.after-returning advice- <aop:after-returning></aop:after-returning>
4.after-throwing advice- <aop:after-throwing></aop:after-throwing>
5.around advice- <aop:around></aop:around>

<aop:config>
   <aop:aspect id="loggingAspect" ref="loggingAspect">
      <aop:pointcut expression="execution(* com.sdnext.aop.service.*.*(..))" id="businessService">
     </aop:pointcut>
      <!-- a before advice definition -->
      <aop:before method="someRequiredTask" pointcut-ref="businessService">
        ..... 
     </aop:before>
      <!-- an after advice definition -->
      <aop:after method="someRequiredTask" pointcut-ref="businessService">
         ...... 
      </aop:after>
      <!-- an after-returning advice definition -->
      <!--The someRequiredTask method must have parameter named retVal -->
      <aop:after-returning method="someRequiredTask" pointcut-ref="businessService" returning="retVal">
        ..... 
      </aop:after-returning>
      <!-- an after-throwing advice definition -->
      <!--The someRequiredTask method must have parameter named ex -->
      <aop:after-throwing method="someRequiredTask" pointcut-ref="businessService" throwing="ex">
        ....
       </aop:after-throwing>
      <!-- an around advice definition -->
      <aop:around method="someRequiredTask" pointcut-ref="businessService">
      ...
     </aop:around>
     .....
  </aop:aspect>
</aop:config>

<bean class="com.sdnext.aop.tutorial.aspectJ.LoggingAspect" name="loggingAspect">
  ....
</bean>

You can use same someRequiredTask or different methods for different advices. These methods will be defined as a part of aspect module.
we can also declare the advice as following ways also.

<aop:config>
   <aop:aspect id="loggingAspect" ref="loggingAspect">
     <aop:after method="stringArgumentsMethods" pointcut="args(name)"></aop:after>
     <aop:after-returning method="afterReturningAdvice" pointcut="args(name)" returning="returnString"></aop:after-returning>
    <aop:after-throwing method="exceptionMethod" pointcut="args(name)" throwing="ex">
</aop:after-throwing>
   <aop:around method="myArroundMethod" pointcut="within(com.sdnext.aop.tutorial.model.Circle)"></aop:around>
   <aop:before method="loggingAdvice" pointcut="execution(public * get*(..))"></aop:before>
 </aop:aspect>
</aop:config>

<bean class="com.sdnext.aop.tutorial.aspectJ.LoggingAspect" name="loggingAspect">
  ....
</bean>

Let see how to use the AspectJ annotation with differences advice in the below interface methods and also we compare the XML schema vs Aspect Annotation.

package com.sdnext.aop.aspectj.tutorial;
 
public interface Employee{
 
 void addEmployee();
 
 String addEmployeeReturnValue();
 
 void addEmployeeThrowException() throws Exception;
 
 void addEmployeeAround(String name);
}


1. AspectJ <aop:before> = @Before

package com.sdnext.aop.tutorial.aspectJ;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class LoggingAspect
{
//Joint point means a place where advice are applied to target methods
@Before("execution(com.sdnext.aop.aspectj.tutorial.Employee.addEmployee(..))")
public void loggingBoforeAdvice(JoinPoint joinPoint)  
{
  System.out.println(joinPoint.toString());
  //....
  //....
  System.out.println("Advice run. ADD method is called");
}

Its Equivalent functionality writing in XML, with <aop:before>.

<!-- Aspect -->
<bean class="com.sdnext.aop.tutorial.aspectJ.LoggingAspect" name="loggingAspect">
 </bean>
<aop:config>
   <aop:aspect id="loggingAspect" ref="loggingAspect">
      <!-- @Before -->
  <aop:pointcut expression="execution(*com.sdnext.aop.aspectj.tutorial.Employee.addEmployee(..))" id="pointCutBefore"></aop:pointcut>
 
     <aop:before method="loggingBoforeAdvice" pointcut-ref="pointCutBefore"></aop:before>
 
  </aop:aspect>
 
</aop:config>


2. AspectJ <aop:after> = @After

package com.sdnext.aop.tutorial.aspectJ;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.After;

@Aspect
public class LoggingAspect
{
//Joint point means a place where advice are applied to target methods
@After("execution(com.sdnext.aop.aspectj.tutorial.Employee.addEmployee(..))")
public void loggingAfterAdvice(JoinPoint joinPoint)  
{
  System.out.println(joinPoint.toString());
  //....
  //....
  System.out.println("Advice run. ADD method is called");
}

Its Equivalent functionality writing in XML, with <aop:after>.

<!-- Aspect -->
<bean class="com.sdnext.aop.tutorial.aspectJ.LoggingAspect" name="loggingAspect">
 </bean>
<aop:config>
   <aop:aspect id="loggingAspect" ref="loggingAspect">
      <!-- @After -->
  <aop:pointcut expression="execution(*com.sdnext.aop.aspectj.tutorial.Employee.addEmployee(..))" id="pointCutAfter"></aop:pointcut>
 
   <aop:before method="loggingAfterAdvice" pointcut-ref="pointCutAfter"></aop:before>
 
  </aop:aspect>
 
</aop:config>


3. AspectJ <aop:after-returning> = @AfterReturning

package com.sdnext.aop.tutorial.aspectJ;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.AfterReturning;

@Aspect
public class LoggingAspect
{
//Joint point means a place where advice are applied to target methods
@AfterReturning(
   pointcut = "execution(* com.sdnext.aop.aspectj.tutorial.Employee.addEmployeeReturnValue(..))",
   returning= "result")
   public void logAfterReturning(JoinPoint joinPoint, Object result) {
 //...
   }
 
}

Its Equivalent functionality writing in XML, with <aop:after-returning>.

<!-- Aspect -->
<bean class="com.sdnext.aop.tutorial.aspectJ.LoggingAspect" name="loggingAspect">
 </bean>
<aop:config>
   <aop:aspect id="loggingAspect" ref="loggingAspect">
      <!-- @AfterReturning -->
    <aop:pointcut expression="execution(* com.sdnext.aop.aspectj.tutorial.Employee.addEmployeeReturnValue(..))" id="pointCutAfterReturning"></aop:pointcut>
 
    <aop:after-returning method="logAfterReturning" pointcut-ref="pointCutAfterReturning" returning="result"> </aop:after-returning>
 
  </aop:aspect>
 
</aop:config>


4. AspectJ <aop:after-throwing> = @AfterThrowing

package com.sdnext.aop.tutorial.aspectJ;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.AfterThrowing;

@Aspect
public class LoggingAspect
{
//Joint point means a place where advice are applied to target methods
@AfterThrowing(
   pointcut = "execution(* com.sdnext.aop.aspectj.tutorial.Employee.addEmployeeThrowException(..))",
    throwing= "error")
   public void logAfterThrowing(JoinPoint joinPoint, Throwable error) {
 //...
  }
}

Its Equivalent functionality writing in XML, with <aop:after-throwing>.

<!-- Aspect -->
<bean class="com.sdnext.aop.tutorial.aspectJ.LoggingAspect" name="loggingAspect">
 </bean>
<aop:config>
   <aop:aspect id="loggingAspect" ref="loggingAspect">
      <!-- @AfterThrowing -->
    <aop:pointcut expression="execution(* com.sdnext.aop.aspectj.tutorial.Employee.addEmployeeThrowException(..))" id="pointCutAfterThrowing">&lt;/
aop:pointcut&gt; 
    <aop:after-thrwoing method="logAfterThrowing" pointcut-ref="pointCutAfterThrowing" throwing="error"></aop:after-thrwoing>
 
  </aop:pointcut></aop:aspect>
 
</aop:config>


5. AspectJ <aop:after-around> = @Around

package com.sdnext.aop.tutorial.aspectJ;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Around;

@Aspect
public class LoggingAspect
{
//Joint point means a place where advice are applied to target methods
@Around("execution(* com.sdnext.aop.aspectj.tutorial.Employee.addEmployeeAround(..))")
 public void logAround(ProceedingJoinPoint joinPoint) throws Throwable {
  //...
 }
}

Its Equivalent functionality writing in XML, with <aop:around>.

<!-- Aspect -->
<bean class="com.sdnext.aop.tutorial.aspectJ.LoggingAspect" name="loggingAspect">
 </bean>
<aop:config>
   <aop:aspect id="loggingAspect" ref="loggingAspect">
     <!-- @Around -->
   <aop:pointcut expression="execution(* com.sdnext.aop.aspectj.tutorial.Employee.addEmployeeAround(..))" id="pointCutAround"></aop:pointcut>
 
   <aop:around method="logAround" pointcut-ref="pointCutAround"></aop:around>
  </aop:aspect>
 
</aop:config>


Spring AOP XML Schema Based Example

To understand above mentioned concepts related to XML Schema Based AOP, let us write an example which will implement few of the advices. To write our example with few advices, let us have working Eclipse IDE in place and follow the following steps to create a Spring application:

Step Description
1 Create a java project with a name SpringAOPDemo and create a package com.dineshonjava.sdnext.aop.aspectJ under the src folder in the created project.
2 Add required Spring libraries using Add External JARs or User Liberaries option .
3 Add Sppring AOP secific libraries aspectjrt.jar, aspectjweaver.jar and aspectj.jar in the project.
4 Create Java classes LoggingAspect, Employee and AopMain under the com.dineshonjava.sdnext.aop.aspect, com.dineshonjava.sdnext.aop.emp,  com.dineshonjava.sdnext.aop package respectively.
5 Create Beans configuration file spring.xml under the src folder.
6 The final step is to create the content of all the Java files and Bean Configuration file and run the application as explained below.

Here is the content of LoggingAspect.java file. This is actually a sample of aspect module which defines methods to be called at various points.

LoggingAspect.java

package com.dineshonjava.sdnext.aop.aspect;

import java.util.Arrays;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

public class LoggingAspect {
 public void logBeforeAdvice(JoinPoint joinPoint) {

  System.out.println("logBeforeAdvice() is running!");
  System.out.println("hijacked : " + joinPoint.getSignature().getName());
  System.out.println("******");
 }

 public void logAfterAdvice(JoinPoint joinPoint) {

  System.out.println("logAfterAdvice() is running!");
  System.out.println("hijacked : " + joinPoint.getSignature().getName());
  System.out.println("******");

 }
 
 public void logAfterReturningAdvice(JoinPoint joinPoint, Object result) {

  System.out.println("logAfterReturningAdvice() is running!");
  System.out.println("hijacked : " + joinPoint.getSignature().getName());
  System.out.println("Method returned value is : " + result);
  System.out.println("******");

 }
 
 public void logAfterThrowingAdvice(JoinPoint joinPoint, Throwable error) {

  System.out.println("logAfterThrowingAdvice() is running!");
  System.out.println("hijacked : " + joinPoint.getSignature().getName());
  System.out.println("Exception : " + error);
  System.out.println("******");

 }
 
 public void logAroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {

  System.out.println("logAroundAdvice() is running!");
  System.out.println("hijacked method : " + joinPoint.getSignature().getName());
  System.out.println("hijacked arguments : " + Arrays.toString(joinPoint.getArgs()));
  
  System.out.println("Around before is running!");
  joinPoint.proceed();
  System.out.println("Around after is running!");
  
  System.out.println("******");

 }
}

Employee.java

package com.dineshonjava.sdnext.aop.emp;

public interface Employee {
 
 void addEmployee();  
    
  String addEmployeeReturnValue();  
    
  void addEmployeeThrowException() throws Exception;  
    
  void addEmployeeAround(String name);  
}

EmployeeImpl.java

package com.dineshonjava.sdnext.aop.emp.impl;

import com.dineshonjava.sdnext.aop.emp.Employee;

public class EmployeeImpl implements Employee {

 public void addEmployee() {
  System.out.println("addEmployee() is running ");
 }

 public String addEmployeeReturnValue() {
  System.out.println("addEmployeeReturnValue() is running ");
  return "abc";
 }

 public void addEmployeeThrowException() throws Exception {
  System.out.println("addEmployeeThrowException() is running ");
  throw new Exception("Generic Error");
 }

 public void addEmployeeAround(String name) {
  System.out.println("addEmployeeAround() is running, args : " + name);
 }
}

spring.xml

<beans xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:>
AopMain.java
package com.dineshonjava.sdnext.aop;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.dineshonjava.sdnext.aop.emp.Employee;

public class AopMain {
 public static void main(String[] args) throws Exception {

  ApplicationContext appContext = new ClassPathXmlApplicationContext("spring.xml");

  Employee employee = (Employee) appContext.getBean("employee");
  employee.addEmployee();
  
  //employee.addEmployeeReturnValue();
  
  //employee.addEmployeeThrowException();
  
  //employee.addEmployeeAround("dinesh");

 }
}

Once you are done with creating source and bean configuration files, let us run the application. If everything is fine with your application, this will print the following message:

Output:
Nov 4, 2012 8:43:29 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@be2358: display name [org.springframework.context.support.ClassPathXmlApplicationContext@be2358]; startup date [Sun Nov 04 20:43:29 IST 2012]; root of context hierarchy
Nov 4, 2012 8:43:30 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [spring.xml]
Nov 4, 2012 8:43:30 PM org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
INFO: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@be2358]: org.springframework.beans.factory.support.DefaultListableBeanFactory@1b383e9
Nov 4, 2012 8:43:30 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1b383e9: defining beans [employee,org.springframework.aop.config.internalAutoProxyCreator,logAspect,
org.springframework.aop.aspectj.AspectJPointcutAdvisor#0,
org.springframework.aop.aspectj.AspectJPointcutAdvisor#1,
org.springframework.aop.aspectj.AspectJPointcutAdvisor#2,
org.springframework.aop.aspectj.AspectJPointcutAdvisor#3,
org.springframework.aop.aspectj.AspectJPointcutAdvisor#4,
pointCutBefore,pointCutAfter,pointCutAfterReturning,pointCutAfterThrowing,pointCutAround];
root of factory hierarchy
logBeforeAdvice() is running!
hijacked : addEmployee
******
addEmployee() is running
logAfterAdvice() is running!
hijacked : addEmployee
******

 

 

 

Previous
Next
Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

2 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

2 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

2 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

2 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

2 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

2 years ago