In this post, We will learn about the Spring AOP around advice using annotation Example using a Demo Project.
One of the very important key units of Spring Framework is the Aspect-oriented programming (AOP) framework. Aspect-Oriented Programming helps us to provide functionalities to break down program logic into distinct parts or modules is called AOP concerns. The functions that spread across multiple sections of an application are called cross-cutting concerns and these cross-cutting concerns functionalities must be separate from the application’s business logic. There are various good examples of aspects like logging, authentication, transaction management, auditing, spring-security, caching, etc.
The key concept of modularity is Object-Oriented Programming is the class, whereas in AOP the unit of modularity is the aspect. Spring dependency Injection usually helps us to lose coupling our application objects from each other and AOP helps us to decouple cross-cutting concerns from the objects that they affect.
Spring AOP provides interceptors to intercept an application. For example, when a method is executed, We can add extra functionality before or after the method execution.
Types of Advice
Spring AOP has five kinds of advice mentioned as Below −
SR.NO | ADVICE & DESCRIPTION |
---|---|
1 | before
This advice executes before a join point, but this advice does not have the ability to prevent execution flow to proceed to the join point (unless it throws an exception). |
2 | after
This advice is executed after a join point completes normally: for example if a method returns without throwing an exception. |
3 | after-returning
This advice runs after the method execution only if the method completes successfully. |
4 | after-throwing
This advice is executed if a method exiting by throwing an exception. |
5 | around
This is the most powerful kind of advice We can use around advice to perform custom behavior before and after the method invocation. This advice is also responsible to select whether to proceed to the joinpoint or to perform the advised method execution by returning its own return value or throwing an exception. |
In this demo project, We will learn about Around Advice using annotations:
pom.xml
We need to add spring and spring-aspects dependencies in Maven pom.xml file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.kkhindigyan.org</groupId> <artifactId>AroundAdviceAnnotationExample</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <org.springframework.version>5.2.13.RELEASE</org.springframework.version> <org.spring.aop.version>5.2.13.RELEASE</org.spring.aop.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${org.spring.aop.version}</version> </dependency> </dependencies> </project> |
Account.java
The model class which hold Account information data
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
package com.kkhindigyan.org.model; public class Account { private String accountNumber; private String accountDescription; public Account(String accountNumber, String accountDescription) { this.accountNumber = accountNumber; this.accountDescription = accountDescription; } public String getAccountNumber() { return accountNumber; } public String getAccountDescription() { return accountDescription; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((accountDescription == null) ? 0 : accountDescription.hashCode()); result = prime * result + ((accountNumber == null) ? 0 : accountNumber.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Account other = (Account) obj; if (accountDescription == null) { if (other.accountDescription != null) return false; } else if (!accountDescription.equals(other.accountDescription)) return false; if (accountNumber == null) { if (other.accountNumber != null) return false; } else if (!accountNumber.equals(other.accountNumber)) return false; return true; } @Override public String toString() { return "Account [accountNumber=" + accountNumber + ", accountDescription=" + accountDescription + "]"; } } |
AccountService.java
AccountService interface
1 2 3 4 5 6 7 8 9 |
package com.kkhindigyan.org.service; import com.kkhindigyan.org.model.Account; import com.kkhindigyan.org.service.exception.AccountNotFoundException; public interface AccountService { public abstract Account updateAccountBalance(Account account, Long amount) throws AccountNotFoundException; } |
AccountServiceImpl.java
AccountService interface implementation class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
package com.kkhindigyan.org.service.impl; import java.util.ArrayList; import java.util.List; import org.springframework.stereotype.Service; import com.kkhindigyan.org.model.Account; import com.kkhindigyan.org.service.AccountService; import com.kkhindigyan.org.service.exception.AccountNotFoundException; @Service("accountService") public class AccountServiceImpl implements AccountService { private static List<Account> accountList = null; static { accountList = new ArrayList<Account>(); accountList.add(new Account("6770865432", "Money tranfer")); accountList.add(new Account("6770865400", "Money tranfer")); } @Override public Account updateAccountBalance(Account account,Long amount) throws AccountNotFoundException{ if(accountList.contains(account)) { System.out.println("Account No:"+account.getAccountNumber()+", Amount:"+amount); return account; }else { throw new AccountNotFoundException("Account Not Found!!"); } } } |
AccountNotFoundException.java
Custom Exception is designed to throw when Account details not found in the repository
1 2 3 4 5 6 7 8 9 |
package com.kkhindigyan.org.service.exception; public class AccountNotFoundException extends Exception { private static final long serialVersionUID = 1L; public AccountNotFoundException(String message) { super(message); } } |
AccountServiceAspect.java
In this java class, we have defined a method aroundAdvice which accepts ProceedingJoinPoint as a method argument and annotated with @Around annotation to mark it as Around Advice.
The Aspects classes are usually Similar to other normal Spring bean and may have methods and fields just like any other class except that they will be annotated with @Aspect.
we have defined a method selectAll () for pointcut that will match the execution of updateAccountBalance(Account account,Long amount) method available in the AccountServiceImpl class under the package com.kkhindigyan.org.service.impl
We can declare either of the five advices using @{AdviceName} annotations as defined in the code below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
package com.kkhindigyan.org.service.aop; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; import com.kkhindigyan.org.model.Account; @Aspect @Component public class AccountServiceAspect { private static Log log = LogFactory.getLog(AccountServiceAspect.class.getName()); @Around(value = "selectAll()") public void aroundAdvice(ProceedingJoinPoint proceedingJoinPoint) { log.info("Before Method:"+proceedingJoinPoint.getSignature()); try { Object object = proceedingJoinPoint.proceed(); if( object instanceof Account) { System.out.println((Account)object); } } catch (Throwable e) { log.error("There is an exception :"+e.toString()); } log.info("After Method:"+proceedingJoinPoint.getSignature()); } @Pointcut("execution(* com.kkhindigyan.org.service.impl.AccountServiceImpl.*(..))") public void selectAll() { } } |
MyConfig.java
This is the Spring Java-based Config file(replacement of applicationContext.xml file). Here We have used @EnableAspectJAutoProxy annotation to enable @AspectJ support in Spring AOP.
1 2 3 4 5 6 7 8 9 10 11 12 |
package com.kkhindigyan.org.config; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.EnableAspectJAutoProxy; @Configuration @EnableAspectJAutoProxy(proxyTargetClass=true) @ComponentScan({"com.kkhindigyan.org.service.aop,com.kkhindigyan.org.service.impl"}) public class MyConfig { } |
ClientTest.java
This ClientTest class gets the AccountService bean from the applicationContext.xml file and calls the updateAccountBalance method of the EmployeeService after that around advice will be called automatically.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
package com.kkhindigyan.org.client; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.support.AbstractApplicationContext; import com.kkhindigyan.org.config.MyConfig; import com.kkhindigyan.org.model.Account; import com.kkhindigyan.org.service.AccountService; import com.kkhindigyan.org.service.impl.AccountServiceImpl; public class ClientTest { public static void main(String[] args) { AbstractApplicationContext ctx = null; try { ctx = new AnnotationConfigApplicationContext(MyConfig.class); AccountService accountService = ctx.getBean("accountService", AccountServiceImpl.class); accountService.updateAccountBalance(new Account("6770865400", "Money tranfer"), 5000L); } catch (Exception e) { e.printStackTrace(); }finally { if(ctx != null) ctx.close(); } } } |
If you run ClientTest.java as Java Application then it will give the below output:
1 2 3 4 5 6 |
Jun 19, 2021 12:06:53 PM com.kkhindigyan.org.service.aop.AccountServiceAspect aroundAdvice INFO: Before Method:Account com.kkhindigyan.org.service.impl.AccountServiceImpl.updateAccountBalance(Account,Long) Account No:6770865400, Amount:5000 Account [accountNumber=6770865400, accountDescription=Money tranfer] Jun 19, 2021 12:06:53 PM com.kkhindigyan.org.service.aop.AccountServiceAspect aroundAdvice INFO: After Method:Account com.kkhindigyan.org.service.impl.AccountServiceImpl.updateAccountBalance(Account,Long) |
That’s all about Spring AOP around advice using annotation Example
You May Also Like:
Spring Dependency Injection with Factory Method
Spring Framework @Qualifier example
Injecting Collections in Spring Framework Example
Spring Bean Definition Inheritance Example
Spring bean scopes with example
Spring JSR-250 Annotations with Example
Spring BeanPostProcessor Example
Spring JDBC Integration Example
Spring JDBC Annotation Example
Spring with Jdbc java based configuration example
Spring JDBC NamedParameterJdbcTemplate Example
How to call stored procedures in the Spring Framework?
Spring 5 and Hibernate 5 integration CRUD Example
Spring HibernateTempate Example
Spring and Hibernate Declarative Transaction Management Example
Spring AOP Concepts and Terminology
Spring AOP Before and After Advice Example
Spring AOP Before and After Advice Using Annotations Example
Spring AOP AfterReturning and AfterThrowing Advice Example
If you have any feedback or suggestion please feel free to drop in below comment box.