How to work with Spring AOP and AspectJ, Aspect, Advice, Pointcut, JoinPoint, Annotation | Spring Boot
Tags: springboot, springaop, aspectj, aspect
Published: 2021-04-29 18:31:12

https://grokonez.com/spring-framework/work-spring-aop-aspectj-aspect-advice-pointcut-joinpoint-annotation-spring-boot

How to work with Spring AOP and AspectJ, Aspect, Advice, Pointcut, JoinPoint, Annotation | Spring Boot

Aspect-Oriented Programming (AOP) helps us cut across many methods, objects and type within the application without embedding it in the business logic. With Spring AOP and AspectJ, you can add additional behaviour to existing code without modifying the code itself.

The tutorial guides you how to work with Spring AOP and AspectJ using Spring Boot.

<div id="tutcontents">
<h2>I. Technology</h2>
- Java 1.8
- Maven 3.3.9
- Spring Tool Suite – Version 3.8.1.RELEASE
- Spring Boot: 1.4.0.RELEASE
<h2>II. Overview</h2>
<h3>1. Goal</h3>
To build an application that uses Aspect on 4 services:
- <strong>CustmerAspect</strong> on <strong>CustomerService</strong>: with simple expression
- <strong>CustmerAspectPointcut</strong> on <strong>CustomerAnotherService</strong>: with <strong>@Pointcut</strong> Annotation and implement Aspect on its own funtions
- <strong>CustmerAdvanceAspect</strong> on <strong>CustomerAdvanceService</strong>: with <strong>@Around</strong> - most powerful kind of advice, and more usages of JoinPoint for getting argument information
- <strong>CustmerAspectAnnotation</strong> on <strong>CustomerLoggableService</strong>: implement Aspect by creating custom annotation <strong>@Loggable</strong>
<h3>2. Project Structure</h3>
<img class="alignnone size-full wp-image-1088" src="https://grokonez.com/wp-content/uploads/2016/09/springaop-aspectj-project-structure.jpg" alt="springaop-aspectj-project-structure" width="700" height="441" />
<h3>3. Step to do</h3>
- Create Spring Boot project &amp; add Dependencies
- Create a DataModel class
- Create Services
- Create Aspect Classes and Inferface
- Create Spring Bean Configuration File
- Create a Web Controller
- Run Spring Boot Application &amp; Enjoy Result
<h3>4. Demo Video</h3>
<iframe width="560" height="315" src="https://www.youtube.com/embed/xj8WO_umY4I" frameborder="0" allowfullscreen></iframe>
<h2>III. Practice</h2>
<h3>1. Create Spring Boot project &amp; add Dependencies</h3>
- Open <strong>Spring Tool Suite</strong>, on <strong>Menu</strong>, choose <strong>File</strong> -&gt; <strong>New </strong>-&gt; <strong>Spring Starter Project</strong>, then fill each fields:
<img class="alignnone size-full wp-image-1089" src="https://grokonez.com/wp-content/uploads/2016/09/springaop-aspectj-startproject.jpg" alt="springaop-aspectj-startproject" width="608" height="429" />

Click <strong>Next</strong>, in <strong>Web</strong>: choose <strong>Web</strong>:
<img class="alignnone size-full wp-image-1091" src="https://grokonez.com/wp-content/uploads/2016/09/springaop-aspectj-configweb.jpg" alt="springaop-aspectj-configweb" width="688" height="95" />

Click <strong>Finish</strong>. Spring Boot project will be created successfully.

- Open <strong>pom.xml</strong>, add dependencies for <strong>Spring Context</strong>, <strong>Spring AOP</strong> and <strong>AspectJ</strong>:

https://grokonez.com/spring-framework/work-spring-aop-aspectj-aspect-advice-pointcut-joinpoint-annotation-spring-boot

How to work with Spring AOP and AspectJ, Aspect, Advice, Pointcut, JoinPoint, Annotation | Spring Boot