<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<h2><b>Advice:</b></h2>
<p>Action taken by an aspect at a particular join point. Different types of advice include &#8220;around,&#8221; &#8220;before&#8221; and &#8220;after&#8221; advice. Advice types are discussed below. Many AOP frameworks, including Spring, model an advice as an interceptor, maintaining a chain of interceptors &#8220;around&#8221; the join point.</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>We&#8217;ll learn about the After Advice types: After (finally), AfterReturning and AfterThrowing.</p>
<pre class="highlight">package com.dineshonjava.Spring.Aop; 
 
import org.springframework.context.ApplicationContext; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
 
import com.dineshonjava.Spring.Aop.service.ShapeService; 
 
public class AopTest { 
 
 public static void main(String args[]) 
 { 
 ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); 
 ShapeService service=context.getBean("shapeService",ShapeService.class); 
 service.getCircle().setNameAndReturning("Dummy Circle return"); 
 } 
} 
</pre>
<pre class="highlight">package com.dineshonjava.Spring.Aop; 
 
import org.aspectj.lang.JoinPoint; 
import org.aspectj.lang.annotation.After; 
import org.aspectj.lang.annotation.AfterReturning; 
import org.aspectj.lang.annotation.AfterThrowing; 
import org.aspectj.lang.annotation.Aspect; 
 
import com.dineshonjava.Spring.Aop.model.Circle; 
 
@Aspect 
public class LoggingAspect { 
 
 public void loggingAdvice(JoinPoint joinpoint) 
 { 
 System.out.println("Run Advice..... get method Executed"); 
 System.out.println(joinpoint.toString()); 
 Circle c=(Circle)joinpoint.getTarget(); 
 System.out.println(c.getName()); 
 } 
 
 @AfterReturning(pointcut="args(name)",returning="returnString") 
 public void allStringMethodArguments(String name, String returnString){ 
 System.out.println("A setter method has been executed............"+name); 
 } 
 
 @AfterThrowing(pointcut="args(name)",throwing="ex") 
 public void exceptionAdvice(String name, Exception ex){ 
 System.out.println("Exception is thrown ............"+ex); 
 } 
 
 @After("args(String)") 
 public void afterAdvice(){ 
 System.out.println("After Advice is executed..........."); 
 } 
} 
</pre>
<p> ;</p>
<pre class="highlight">package com.dineshonjava.Spring.Aop; 
 
public class Circle { 
 
 private String name; 
 
 public String getName() { 
 return name; 
 } 
 
 public void setName(String name) { 
 this.name = name; 
 System.out.println("Setter method is called....."); 
 throw(new RuntimeException()); 
 } 
 
 public String setNameAndReturning(String name) { 
 this.name = name; 
 System.out.println("Setter Method and return the value method executed...."); 
 return name; 
 } 
} 
</pre>
<pre class="highlight">package com.dineshonjava.Spring.Aop; 
 
public class Triangle { 
 
 private String name; 
 
 public String getName() { 
 return name; 
 } 
 
 public void setName(String name) { 
 this.name = name; 
 } 
} 
</pre>
<pre class="highlight">package com.dineshonjava.Spring.Aop; 
 
import com.dineshonjava.Spring.Aop.model.Circle; 
import com.dineshonjava.Spring.Aop.model.Triangle; 
 
public class ShapeService { 
 
 private Triangle triangle; 
 private Circle circle; 
 public Triangle getTriangle() { 
 return triangle; 
 } 
 public void setTriangle(Triangle triangle) { 
 this.triangle = triangle; 
 } 
 public Circle getCircle() { 
 return circle; 
 } 
 public void setCircle(Circle circle) { 
 this.circle = circle; 
 } 
} 
</pre>
<pre class="highlight"><;beans xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd 
 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd 
 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">; 
<;aop:aspectj-autoproxy>; 
 <;bean class="com.dineshonjava.Spring.Aop.model.Triangle" name="triangle">; 
 <;property name="name" value="triangle name">; 
 <;/property>;<;/bean>; 
 <;bean class="com.dineshonjava.Spring.Aop.model.Circle" name="circle">; 
<;!-- <;property name="name" value="circle name"/>; -->; 
 <;/bean>; 
 <;bean class="com.dineshonjava.Spring.Aop.service.ShapeService" name="shapeService">; 
 <;property name="triangle" ref="triangle">; 
 <;property name="circle" ref="circle">; 
 <;/property>;<;/property>;<;/bean>; 
 <;bean class="com.dineshonjava.Spring.Aop.Aspect.LoggingAspect" name="loggingAdvice">; 
<;/bean>;<;/aop:aspectj-autoproxy>;<;/beans>; 
</pre>
<h2 class="post-title entry-title">Around Advice Type</h2>
<div class="post-header"></div>
<div style="font-family: Verdana,sans-serif;">This example covers the Around advice type. We&#8217;ll learn how to use it, and we&#8217;ll also look at some of the unique and powerful features that are specific to this advice type.</div>
<pre class="highlight">package com.dineshonjava.Spring.Aop; 
 
import org.springframework.context.ApplicationContext; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
 
import com.dineshonjava.Spring.Aop.service.ShapeService; 
 
public class AopTest { 
 
 public static void main(String args[]) 
 { 
 ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); 
 ShapeService service=context.getBean("shapeService",ShapeService.class); 
 service.getCircle(); 
 } 
} 
</pre>
<pre class="highlight">package com.dineshonjava.Spring.Aop; 
 
import org.aspectj.lang.JoinPoint; 
import org.aspectj.lang.ProceedingJoinPoint; 
import org.aspectj.lang.annotation.After; 
import org.aspectj.lang.annotation.AfterReturning; 
import org.aspectj.lang.annotation.AfterThrowing; 
import org.aspectj.lang.annotation.Around; 
import org.aspectj.lang.annotation.Aspect; 
import org.aspectj.lang.annotation.Pointcut; 
 
import com.dineshonjava.Spring.Aop.model.Circle; 
 
@Aspect 
public class LoggingAspect { 
 
 public void loggingAdvice(JoinPoint joinpoint) 
 { 
 System.out.println("Run Advice..... get method Executed"); 
 System.out.println(joinpoint.toString()); 
 Circle c=(Circle)joinpoint.getTarget(); 
 System.out.println(c.getName()); 
 } 
 
 @AfterReturning(pointcut="args(name)",returning="returnString") 
 public void allStringMethodArguments(String name, String returnString){ 
 System.out.println("A setter method has been executed............"+name); 
 } 
 
 @AfterThrowing(pointcut="args(name)",throwing="ex") 
 public void exceptionAdvice(String name, Exception ex){ 
 System.out.println("Exception is thrown ............"+ex); 
 } 
 
 @After("args(String)") 
 public void afterAdvice(){ 
 System.out.println("After Advice is executed..........."); 
 } 
 
 @Pointcut("execution(* get*())") 
 public void allGetters(){} 
 
 
 @Around("allGetters()") 
 public Object myAroundAdvice(ProceedingJoinPoint proceedingJoinPoint) 
 { 
 Object returnValue=null; 
 try { 
 System.out.println("Before method Executed"); 
 returnValue=proceedingJoinPoint.proceed(); 
 System.out.println("After Method Executed...."); 
 } catch (Throwable e) { 
 System.out.println("Around Advice throws exception"); 
 } 
 System.out.println("After Finally executed........"); 
 
 return returnValue; 
 } 
 } 
</pre>
<pre class="highlight">package com.dineshonjava.Spring.Aop; 
 
public class Circle { 
 
 private String name; 
 
 public String getName() { 
 return name; 
 } 
 
 public void setName(String name) { 
 this.name = name; 
 System.out.println("Setter method is called....."); 
 throw(new RuntimeException()); 
 } 
 
 public String setNameAndReturning(String name) { 
 this.name = name; 
 System.out.println("Setter Method and return the value method executed...."); 
 return name; 
 } 
} 
</pre>
<pre class="highlight">package com.dineshonjava.Spring.Aop; 
 
public class Triangle { 
 
 private String name; 
 
 public String getName() { 
 return name; 
 } 
 
 public void setName(String name) { 
 this.name = name; 
 } 
} 
</pre>
<pre class="highlight"><;beans xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd 
 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd 
 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">; 
<;aop:aspectj-autoproxy>; 
 <;bean class="com.dineshonjava.Spring.Aop.model.Triangle" name="triangle">; 
 <;property name="name" value="triangle name">; 
 <;/property>;<;/bean>; 
 <;bean class="com.dineshonjava.Spring.Aop.model.Circle" name="circle">; 
<;!-- <;property name="name" value="circle name"/>; -->; 
 <;/bean>; 
 <;bean class="com.dineshonjava.Spring.Aop.service.ShapeService" name="shapeService">; 
 <;property name="triangle" ref="triangle">; 
 <;property name="circle" ref="circle">; 
 <;/property>;<;/property>;<;/bean>; 
 <;bean class="com.dineshonjava.Spring.Aop.Aspect.LoggingAspect" name="loggingAdvice">; 
<;/bean>;<;/aop:aspectj-autoproxy>;<;/beans>; 
</pre>
<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-after-throwing-advice-example-using-xml-config/"><span style="color: red;"> Spring AOP After Throwing Advice Example using XML Config</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 style="text-align: center;"><b><;<; </b><b><b><a href="https://dineshonjava.com/joinpoints-and-advice-arguments-chapter/">JoinPoints and Advice Arguments</a></b></b><b><b></b></b><b> |<a href="https://dineshonjava.com/spring-tutorial/">index</a>| </b><b><b><a href="https://dineshonjava.com/aop-xml-configuration-chapter-30/">AOP XML configuration</a></b>>;>;</b></div>
</div>
<p> ;</p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/joinpoints-and-advice-arguments-chapter/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/aop-xml-configuration-chapter-30/">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: '640'});
});
</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…