Spring AOP After-Returning Advice Example using XML Config

In this spring aop after-returning advice example with XML configuration, we learn how to use Spring AOP after-returning advice using <aop:after-returning/> XML configuration. In Spring AOP, After-returning Advice to be executed if a join point exits normally without any error/exceptional i.e  Any methods configured as After-returning advice always run immediately after the target methods return executed normally.

Download Application Source Code

Spring AOP After-returning Advice Example using XML Config from GitHub.

Let’s create a simple spring application and add logging aspect to be invoked on based on pointcuts information passed in <aop:after-returning/> xml configuration. This example is also available with Java configuration Spring AOP AspectJ @AfterReturning Annotation Advice Example.

Configuring Spring AOP After-returning Advice using aop namespace <aop:after-returning/>

In this example, we are using <aop:*/> namespace for XML configuration. So here we add <aop:after-returning/> aop namespace in our XML configuration file in this example. Let’s see our aop configuration for after advice in this example.

 

<aop:config>
  <aop:aspect ref="loggingAspect">
    <!-- all public methods with any arguments of any type and any return type of all classes in the com.doj.aopapp.service package -->
    <aop:pointcut expression="execution(* com.doj.aopapp.service.*.*(..))" id="logForAllMethods"/>
    <!-- all public methods whose name are transfer() with taking three arguments of any type and any return type of all classes in the com.doj.aopapp.service package -->
    <aop:pointcut expression="execution(* com.doj.aopapp.service.*.transfer(*,*,*))" id="logForAllTransfer"/>
    <aop:after-returning method="afterReturningAdviceForAllMethods" pointcut-ref="logForAllMethods"/>
    <aop:after-returning method="afterReturningAdviceForTransferMethods" pointcut-ref="logForAllTransfer"/>
   </aop:aspect>
</aop:config>

Declaring Pointcut expressions

#1. In First pointcut expression, we have declared after-returning advice, it is valid for all public methods with any number of arguments of any type and any return type, for all classes in the com.doj.aopapp.service package.

<aop:pointcut expression="execution(* com.doj.aopapp.service.*.*(..))" id="logForAllMethods"/>

#2. In Second pointcut expression, we have declared after-returning advice, it is valid for all public methods whose name is transfer() with taking three arguments of any type and any return type, for all classes in the com.doj.aopapp.service package.

<aop:pointcut expression="execution(* com.doj.aopapp.service.*.transfer(*,*,*))" id="logForAllTransfer"/>

Spring AOP After-returning Advice Example

Let’s create an example for a after-returning advice, using xml configuration using <aop:after-returning/> namespace.

Spring AOP Maven Dependencies

<properties>
   <spring.version>4.3.7.RELEASE</spring.version>
   <aspectj.version>1.8.9</aspectj.version>
  </properties>
  
  <dependencies>
   <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-context</artifactId>
         <version>${spring.version}</version>
     </dependency>
     <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-context-support</artifactId>
         <version>${spring.version}</version>
     </dependency>
     <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-aop</artifactId>
         <version>${spring.version}</version>
     </dependency>
     
     <dependency>
          <groupId>org.aspectj</groupId>
          <artifactId>aspectjrt</artifactId>
          <version>${aspectj.version}</version>
      </dependency>
      <dependency>
          <groupId>org.aspectj</groupId>
          <artifactId>aspectjweaver</artifactId>
          <version>${aspectj.version}</version>
      </dependency>
  </dependencies>

ApplicationContext Configuration file based on XML Config

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

 <aop:config>
  <aop:aspect ref="loggingAspect">
   <!-- all public methods with any arguments of any type and any return type of all classes in the com.doj.aopapp.service package -->
   <aop:pointcut expression="execution(* com.doj.aopapp.service.*.*(..))" id="logForAllMethods"/>
   <!-- all public methods whose name are transfer() with taking three arguments of any type and any return type of all classes in the com.doj.aopapp.service package -->
   <aop:pointcut expression="execution(* com.doj.aopapp.service.*.transfer(*,*,*))" id="logForAllTransfer"/>
   <aop:after-returning method="afterReturningAdviceForAllMethods" pointcut-ref="logForAllMethods"/>
   <aop:after-returning method="afterReturningAdviceForTransferMethods" pointcut-ref="logForAllTransfer"/>
  </aop:aspect>
 </aop:config>
 
 <bean id="transferService" class="com.doj.aopapp.service.TransferServiceImpl"/>
 
 <bean id="loggingAspect" class="com.doj.aopapp.aspect.LoggingAspect"/>
</beans>

TransferService.java

/**
 * 
 */
package com.doj.aopapp.service;

/**
 * @author Dinesh.Rajput
 *    
 */
public interface TransferService {
 
 void transfer(String accountA, String accountB, Long amount);
 
 Double checkBalance(String account);
 
 Long withdrawal(String account, Long amount);
 
 void diposite(String account, Long amount);
}

TransferServiceImpl.java

/**
 * 
 */
package com.doj.aopapp.service;

/**
 * @author Dinesh.Rajput
 *
 */
public class TransferServiceImpl implements TransferService {

 @Override
 public void transfer(String accountA, String accountB, Long amount) {
  System.out.println(amount+" Amount has been tranfered from "+accountA+" to "+accountB);
 }

 @Override
 public Double checkBalance(String account) {
  System.out.println("Available balance: 50000");
  return new Double(50000);
 }

 @Override
 public Long withdrawal(String account, Long amount) {
  System.out.println("Withdrawal amount: " +amount);
  return amount;
 }

 @Override
 public void diposite(String account, Long amount) {
  System.out.println(amount+" Amount has been diposited to "+account);
 }

}

LoggingAspect.java

/**
 * 
 */
package com.doj.aopapp.aspect;

import org.aspectj.lang.JoinPoint;

/**
 * @author Dinesh.Rajput
 *
 */
public class LoggingAspect {
 
 /**
  * Declaring after-returning advice 
  * @param jp
  * @throws Throwable
  */
 public void afterReturningAdviceForAllMethods(JoinPoint jp) throws Throwable {
        System.out.println("****LoggingAspect.afterReturningAdviceForAllMethods() " + jp.getSignature().getName());
    }
 
 /**
  * Declaring after-returning advice for all transfer methods whose taking three arguments of any type 
  * of all classes in the package com.doj.aopapp.service
  * @param jp
  * @throws Throwable
  */
 public void afterReturningAdviceForTransferMethods(JoinPoint jp) throws Throwable {
        System.out.println("****LoggingAspect.afterReturningAdviceForTransferMethods() " + jp.getSignature().getName());
    }
}

Test Class for Spring AOP After-returning Advice Configuration and Execution

Let’s execute following test class and analyse the output on the console.

/**
 * 
 */
package com.doj.aopapp.test;

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

import com.doj.aopapp.service.TransferService;

/**
 * @author Dinesh.Rajput
 *
 */
public class Main {

 /**
  * @param args
  */
 public static void main(String[] args) {
  ConfigurableApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
  TransferService transferService = applicationContext.getBean(TransferService.class);
  transferService.transfer("accountA", "accountB", 50000l);
  transferService.checkBalance("accountA");
  transferService.diposite("accountA",  50000l);
  transferService.withdrawal("accountB", 40000l);
  applicationContext.close();
 }

}

Output on the Console:

Mar 09, 2017 12:52:35 AM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@179d3b25: startup date [Thu Mar 09 00:52:35 IST 2017]; root of context hierarchy
Mar 09, 2017 12:52:35 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
50000 Amount has been tranfered from accountA to accountB
****LoggingAspect.afterReturningAdviceForAllMethods() transfer
****LoggingAspect.afterReturningAdviceForTransferMethods() transfer
Available balance: 50000
****LoggingAspect.afterReturningAdviceForAllMethods() checkBalance
50000 Amount has been diposited to accountA
****LoggingAspect.afterReturningAdviceForAllMethods() diposite
Withdrawal amount: 40000
****LoggingAspect.afterReturningAdviceForAllMethods() withdrawal
Mar 09, 2017 12:52:36 AM org.springframework.context.support.ClassPathXmlApplicationContext doClose
INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@179d3b25: startup date [Thu Mar 09 00:52:35 IST 2017]; root of context hierarchy

Project Structure

Spring AOP After-Returning Advice Example using XML Config

 

Spring AOP Related Posts

 

 

 

Previous
Next