Spring AOP Examples
Create a maven project and add following dependencies.
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sks</groupId>
<artifactId>aoptutorial</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.7.4</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>1.7.4</version>
</dependency>
</dependencies>
</project>
Before Advice example
Let’s create one class called Employee.
package com.sks;
public class Employee {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
EmployeeBeforeAspect.java
package com.sks.aspect;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class EmployeeBeforeAspect {
//In the below execution expression * means any return type
@Before("execution(* com.sks.Employee.getName())")
public void getNameBeforeAdvice() {
System.out.println("Executing \"Advice\" before getName method of Employee class.");
}
}
applicationContext.xml file
<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<bean id="emp" class="com.sks.Employee">
<property name="name" value="Arjun"></property>
</bean>
<bean id="empBeforeAspect" class="com.sks.aspect.EmployeeBeforeAspect" />
</beans>
Main method class for testing
package com.sks;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class EmployeeTesting {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Employee e = (Employee) context.getBean("emp");
e.setName("Arjun");
System.out.println("Name : " + e.getName());
context.close();
}
}
Output:
In the below example, you can see that before getName() method is invoked on Employee object ‘e’, getNameBeforeAdvice() has been executed.
More AOP expressions
@Before("execution(* com.sks.Employee.get*())")
Defining point cut and join point
We can define point cut expressions like below.
package com.sks.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class EmployeeBeforeAspect {
@Before("getNamePointCut()")
public void getNamePointcutAdvice() {
System.out.println("Executing pointcut advice for getEmployee method.");
}
@Before("getNamePointCut())")
public void getNameJointPointAdvice(JoinPoint joinPoint) {
System.out.println("Executing join point advice at location : " + joinPoint.toString());
}
@Pointcut("execution(* com.sks.service.EmployeeService.getEmployee())")
public void getNamePointCut() {
}
}
After Advice example
package com.sks.aspect;
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;
@Aspect
public class EmployeeAfterAspect {
@After("args(name,surname)")
public void getNameAfterAdvice(String name, String surname) {
System.out.println("After advice being executed.");
System.out.println("Passed argument are : " + name + " " + surname);
}
}
After Throwing Advice example
In this example, we will throw one exception forcefully so that the advice is executed
package com.sks;
public class Employee {
private String name;
private String surName;
public void setName(String name) throws Exception {
this.name = name;
if(name.equals("Amar")) {
throw new Exception();
}
}
public void setFullName(String name, String surName) {
this.name = name;
this.surName = surName;
}
//getter and setter methods
@Override
public String toString() {
return "Employee [name=" + name + ", surName=" + surName + "]";
}
}
package com.sks.aspect;
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;
@Aspect
public class EmployeeAfterAspect {
@AfterThrowing("within(com.sks..*)")
public void logException(JoinPoint joinPoint) {
System.out.println("Exception is thrown in class at : " + joinPoint.toString());
}
}
Around Advice example
package com.sks.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
@Aspect
public class EmployeeAroundAspect {
@Around("execution(* *.getName())")
public Object getNameAroudAdvice(ProceedingJoinPoint proceedingJoinPoint) {
Object result = null;
System.out.println("Before executing around advice for getName method.");
try {
result = proceedingJoinPoint.proceed();
} catch (Throwable e) {
e.printStackTrace();
}
System.out.println("After executing around advice for getName method.");
return result;
}
}