<div dir="ltr" style="text-align: justify;">
<p>In this spring aop <b>after-throwing</b> advice example with XML configuration, we learn how to use Spring AOP after-throwing advice using <b><;aop:after-throwing/>;</b> XML configuration. In Spring AOP, After-throwing Advice to be executed if a method exits by throwing an exception i.e Any methods configured as After-throwing advice always run immediately after the target methods throws any exception.<br />
<b><br />
</b></p>
<h2 id="ads-id" align="center"> <b>Download Application Source Code</b></h2>
<h3><a href="https://github.com/DOJ-SoftwareConsultant/SpringAOP-AfterThrowing-Advice-XML" target="_blank" rel="noopener"><b>Spring AOP After Throwing Advice Example using XML Config on GitHub.</b></a></h3>
<p>Let&#8217;s create a simple spring application and add logging aspect to be invoked on based on pointcuts information passed in <b><;aop:after-throwing/>;</b> xml configuration. This example is also available with Java configuration <b><a href="https://dineshonjava.com/spring-aop-aspectj-after-throwing-annotation-advice-example/">Spring AOP AspectJ @AfterThrowing Annotation Advice Example</a></b>.</p>
<h3><b>Configuring Spring AOP After Throwing Advice using aop namespace <;aop:after-throwing/>;</b></h3>
<p>In this example, we are using <b><;aop:*/>; </b>namespace for XML configuration. So here we have to add <b><;aop:after-throwing/>;</b> aop namespace in our XML configuration file in this example. Let&#8217;s see our aop configuration for after advice in this example.</p>
<div style="background-color: #f2f9fc; border: 1px solid #c9e6f2; border-radius: 3px; padding: 16px; line-height: 1.45;"><span style="color: red; font-size: x-large; text-align: center;"><b>Popular Tutorials</b></span></p>
<ul style="text-align: left;">
<li><b><a href="https://dineshonjava.com/spring-tutorial/"><em><strong>Spring Tutorial</strong> </em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-web-mvc-framework-chapter-38/"><strong><em>Spring MVC Web Tutorial </em></strong></a></b></li>
<li><b><a href="https://dineshonjava.com/introduction-to-spring-boot-a-spring-boot-complete-guide/"><strong>Spring Boot Tutorial</strong> </a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-take-baby-step-to-secure/"><em>Spring Security Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-tutorial-with-example-aspect-advice-pointcut-joinpoint/"><em>Spring AOP Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/using-spring-jdbc-framework-chapter-32/"><em>Spring JDBC Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-hateoas-hypermedia-driven-restful-web-service/"><em><strong>Spring HATEOAS </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/microservices-with-spring-boot/"><em><strong>Microservices with Spring Boot</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/jax-rs-web-service-tutorial/"><strong><em>REST Webservice</em> </strong></a></b></li>
<li><b><a href="https://dineshonjava.com/core-java-baby-step-to-be-best-java-ian/"><em><strong>Core Java </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/hibernate-3-on-baby-steps/"><em><strong>Hibernate Tutorial</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-batch-process-with-example/"><strong><em>Spring Batch</em> </strong></a></b></li>
</ul>
</div>
<p> ;</p>
<pre class="highlight"><;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-throwing method="afterThrowingAdviceForAllMethods" pointcut-ref="logForAllMethods" throwing="exc"/>; 
 <;aop:after-throwing method="afterThrowingAdviceForTransferMethods" pointcut-ref="logForAllTransfer" throwing="exc"/>; 
 <;/aop:aspect>; 
<;/aop:config>; 
</pre>
<p>As in above configuration throwing attribute enable to capture thrown exception instance from advised method. Here we have passed exc named parameter which needs to be passed to advice method.</p>
<h3><b>Declaring Pointcut expressions</b></h3>
<p><b>#1.</b> In First pointcut expression, we have declared after throwing 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.</p>
<pre class="highlight"><;aop:pointcut expression="execution(* com.doj.aopapp.service.*.*(..))" id="logForAllMethods"/>; 
</pre>
<p><b>#2.</b> In Second pointcut expression, we have declared after throwing 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.</p>
<pre class="highlight"><;aop:pointcut expression="execution(* com.doj.aopapp.service.*.transfer(*,*,*))" id="logForAllTransfer"/>; 
</pre>
<h2><b>Spring AOP After Throwing Advice Example </b></h2>
<p>Let&#8217;s create an example for a after throwing advice, using xml configuration using <b><;aop:after-throwing/>;</b> namespace.</p>
<h3><b>Spring AOP Maven Dependencies</b></h3>
<pre class="highlight"><;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>; 
 
</pre>
<h3><b>ApplicationContext Configuration file based on XML Config </b></h3>
<p><b>applicationContext.xml</b></p>
<pre class="highlight"><;?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-throwing method="afterThrowingAdviceForAllMethods" pointcut-ref="logForAllMethods" throwing="exc"/>; 
 <;aop:after-throwing method="afterThrowingAdviceForTransferMethods" pointcut-ref="logForAllTransfer" throwing="exc"/>; 
 <;/aop:aspect>; 
 <;/aop:config>; 
 
 <;bean id="transferService" class="com.doj.aopapp.service.TransferServiceImpl"/>; 
 
 <;bean id="loggingAspect" class="com.doj.aopapp.aspect.LoggingAspect"/>; 
<;/beans>; 
 
</pre>
<h3><b>Target method of Service class on which aspects needs to apply</b></h3>
<p><b>TransferService.java</b></p>
<pre class="highlight">/** 
 * 
 */ 
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); 
} 
 
</pre>
<p><b>TransferServiceImpl.java</b></p>
<pre class="highlight">/** 
 * 
 */ 
package com.doj.aopapp.service; 
 
/** 
 * @author Dinesh.Rajput 
 * 
 */ 
public class TransferServiceImpl implements TransferService { 
 
 @Override 
 public Double checkBalance(String account) { 
 System.out.println("Available balance: 50000"); 
 return new Double(50000); 
 } 
 
 @Override 
 public void transfer(String accountA, String accountB, Long amount) { 
 System.out.println(amount+" Amount trasferring from "+accountA+" to "+accountB); 
 throw new NullPointerException("Opps something went wrong!!!"); 
 } 
 
 @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); 
 } 
 
} 
 
</pre>
<p><b>Aspect class &#8220;LoggingAspect&#8221;:</b><br />
Write aspect class and methods to be executed as advice.<br />
<b>LoggingAspect.java</b></p>
<pre class="highlight">/** 
 * 
 */ 
package com.doj.aopapp.aspect; 
 
import org.aspectj.lang.JoinPoint; 
 
/** 
 * @author Dinesh.Rajput 
 * 
 */ 
public class LoggingAspect { 
 
 /** 
 * Declaring After Throwing advice 
 * @param jp 
 * @throws Throwable 
 */ 
 public void afterThrowingAdviceForAllMethods(JoinPoint jp, Exception exc) throws Throwable { 
 System.out.println("****LoggingAspect.afterThrowingAdviceForAllMethods() " + jp.getSignature().getName()+" Exception "+exc); 
 } 
 
 /** 
 * Declaring After Throwing 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 afterThrowingAdviceForTransferMethods(JoinPoint jp, Exception exc) throws Throwable { 
 System.out.println("****LoggingAspect.afterThrowingAdviceForTransferMethods() " + jp.getSignature().getName()+" Exception "+exc); 
 } 
 
} 
</pre>
<h3><b>Test Class for Spring AOP After Throwing Advice Configuration and Execution</b></h3>
<p>Let&#8217;s execute following test class and analyse the output on the console.</p>
<pre class="highlight">/** 
 * 
 */ 
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(); 
 } 
 
} 
 
</pre>
<h2><b>Output on the Console:</b></h2>
<div style="background-color: #f2f9fc; border-radius: 3px; border: 1px solid #c9e6f2; line-height: 1.45; padding: 16px;">
<p>Mar 09, 2017 9:11:15 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh<br />
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@179d3b25: startup date [Thu Mar 09 21:11:15 IST 2017]; root of context hierarchy<br />
Mar 09, 2017 9:11:15 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions<br />
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]<br />
50000 Amount trasferring from accountA to accountB<br />
****LoggingAspect.afterThrowingAdviceForAllMethods() transfer <b>Exception java.lang.NullPointerException: Opps something went wrong!!!</b><br />
****LoggingAspect.afterThrowingAdviceForTransferMethods() transfer <b>Exception java.lang.NullPointerException: Opps something went wrong!!!</b><br />
Exception in thread &#8220;main&#8221; java.lang.NullPointerException: <b>Opps something went wrong!!!</b><br />
at com.doj.aopapp.service.TransferServiceImpl.transfer(TransferServiceImpl.java:21)<br />
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)<br />
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)<br />
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)<br />
at java.lang.reflect.Method.invoke(Unknown Source)<br />
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:333)<br />
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)<br />
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)<br />
at org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:62)<br />
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)<br />
at org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:62)<br />
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)<br />
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)<br />
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)<br />
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)<br />
at com.sun.proxy.$Proxy2.transfer(Unknown Source)<br />
at com.doj.aopapp.test.Main.main(Main.java:23)</p>
</div>
<p>after throwing aspect advices executed on relevant jointpoints.</p>
<h2><b>Project Structure</b></h2>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2017/03/spring-aop-after-throwing-advice-xml-example.jpg" border="0" /></div>
<p> ;</p>
<div style="background-color: #f2f9fc; border-radius: 3px; border: 1px solid #c9e6f2; line-height: 1.45; padding: 16px;"><span style="color: red; font-size: x-large; text-align: center;"><b>Spring AOP Related Posts</b></span></p>
<ul>
<li><b><a href="https://dineshonjava.com/spring-aop-interview-questions-and-answers/"><span style="color: red;">Spring AOP Interview Questions and Answers</span></a></b></li>
<li><b><a href="https://dineshonjava.com/introduction-to-aop-in-spring/"><span style="color: red;">Spring AOP-Introduction to Aspect Oriented Programming</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aspect-annotation/"><span style="color: red;">@Aspect Annotation in Spring</span></a></b></li>
<li><b><a href="https://dineshonjava.com/after-advice-type-and-around-advice/"><span style="color: red;">Advices in Spring AOP </span></a></b></li>
<li><b><a href="https://dineshonjava.com/joinpoints-and-advice-arguments-chapter/"><span style="color: red;">Spring AOP JoinPoints and Advice Arguments</span></a></b></li>
<li><b><a href="https://dineshonjava.com/pointcuts-and-wildcard-expressions/"><span style="color: red;">Spring AOP-Declaring pointcut Expressions with Examples</span></a></b></li>
<li><b><a href="https://dineshonjava.com/aop-xml-configuration-chapter-30/"><span style="color: red;">Spring AOP XML configuration</span></a></b></li>
<li><b><a href="https://dineshonjava.com/xml-schema-based-aop-example/"><span style="color: red;"> Spring AOP XML Schema based Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-aspectj-before-annotation-advice-example/"><span style="color: red;"> Spring AOP AspectJ @Before Annotation Advice Example<br />
</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-before-advice-example-using-xml-config/"><span style="color: red;"> Spring AOP Before Advice Example using XML Config</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-aspectj-after-annotation-advice-example/"><span style="color: red;"> Spring AOP AspectJ @After Annotation Advice Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-after-advice-example-using-xml-config/"><span style="color: red;"> Spring AOP After Advice Example using XML Config</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-aspectj-after-returning-annotation-advice-example/"><span style="color: red;"> Spring AOP AspectJ @AfterReturning Annotation Advice Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-after-returning-advice-example-using-xml-config/"><span style="color: red;"> Spring AOP After-Returning Advice Example using XML Config</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-aspectj-after-throwing-annotation-advice-example/"><span style="color: red;"> Spring AOP AspectJ @AfterThrowing Annotation Advice Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-aspectj-around-annotation-advice-example/"><span style="color: red;"> Spring AOP AspectJ @Around Annotation Advice Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-around-advice-example-using-xml-config/"><span style="color: red;"> Spring AOP Around Advice Example using XML Config</span></a></b></li>
<li><b><a href="https://dineshonjava.com/writing-first-aspectj-program-in-spring/"><span style="color: red;">Spring AOP Writing First AspectJ Program in Spring</span></a></b></li>
<li><b><a href="https://dineshonjava.com/understanding-aop-proxies-chapter-31/"><span style="color: red;">Spring AOP Proxies in Spring</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-transaction-management-in/"><span style="color: red;"> Spring AOP Transaction Management in Hibernate</span></a></b></li>
<li><b><a href="https://dineshonjava.com/transaction-management-in-spring/"><span style="color: red;">Spring Transaction Management</span></a></b></li>
<li><b><a href="https://dineshonjava.com/declarative-transaction-management/"><span style="color: red;">Spring Declarative Transaction Management Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-ordering-of-aspects-with-example/"><span style="color: red;">Spring AOP-Ordering of Aspects with Example</span></a></b></li>
</ul>
<p> ;</p>
</div>
<p> ;</p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/spring-aop-aspectj-after-throwing-annotation-advice-example/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/spring-aop-aspectj-around-annotation-advice-example/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '47'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…