AspectJ是主流的AOP框架,Spring在其AOP中支持AspectJ注解编写的POJO aspect。
首先我们需要在Bean配置文件中启用,只要使用标签<aop:aspectj-autoproxy>
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
">
<!-- 添加注解功能 -->
<context:annotation-config/>
<!-- 添加自动扫描 -->
<context:component-scan base-package="com.lkt.entity.*"></context:component-scan>
<!-- 添加面向切面功能 -->
<aop:aspectj-autoproxy/>
</beans>
然后写一个普通的接口
package com.lkt.entity.aspectj;
public interface IArithmeticCalculator {
public double add(double a, double b);
public double sub(double a, double b);
public double mul(double a, double b);
public double div(double a, double b);
}
写实现接口的类,添加Spring的组件注解,可以让Spring自动扫描到
package com.lkt.entity.aspectj;
import org.springframework.stereotype.Component;
@Component
public class ArithmeticCalculator implements IArithmeticCalculator {
@Override
public double add(double a,double b){
System.out.println("加法运算:"+(a+b));
return a+b;
}
@Override
public double sub(double a, double b) {
System.out.println("减法运算:"+(a-b));
return a-b;
}
@Override
public double mul(double a, double b) {
System.out.println("乘法运算:"+(a*b));
return a*b;
}
@Override
public double div(double a, double b) {
System.out.println("除法运算:"+(a/b));
return a/b;
}
}
然后编写一个aspect类,用来对ArithmeticCalculator进行前置和后置通知
package com.lkt.entity.aspectj;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class LogTest {
@Before("execution(* IArithmeticCalculator.add(..))")
public void logBefore(){
System.out.println("前置通知");
}
@AfterReturning("execution(* IArithmeticCalculator.add(..))")
public void logAfter(){
System.out.println("后置通知");
}
}
@Aspect表示为一个aspect类,@Before("execution(* IArithmeticCalculator.add(..))"),表示执行IArithmeticCalculator的add方法之前要执行这个方法,@AfterReturning为执行结束后再执行这个方法,*代表任何修饰符,后面的两个点表示任何数量的参数
然后我们在执行add方法的时候就会打印
前置通知
加法运算:2.0
后置通知
如果你想在执行切入方法时,想知道是执行哪个方法时调用到该切入方法的,你可以使用JoinPoint这个对象实例。
后置通知中可以在@AfterReturning注解中添加一个returning属性,访问连接点的返回值,用于传入返回值。
package com.lkt.entity.aspectj;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class LogTest {
@Before("execution(* IArithmeticCalculator.*(..))")
public void logBefore(JoinPoint jp){
String name=jp.getSignature().getName();
System.out.println("前置通知,方法"+name);
}
@AfterReturning(pointcut="execution(* IArithmeticCalculator.*(..))",returning="result")
public void logAfterReturning(JoinPoint jp,Object result){
String name=jp.getSignature().getName();
System.out.println("后置通知,方法:"+name+",返回值:"+result);
}
@After("execution(* IArithmeticCalculator.*(..))")
public void logAfter(JoinPoint jp){
System.out.println("最终通知");
}
}
如果感觉每次都要定义重复的切入点很麻烦时,AspectJ也提供独立定义切入点,在多个通知中重用
package com.lkt.entity.aspectj;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class LogTest {
@Pointcut("execution(* IArithmeticCalculator.*(..))")
private void logginOperation(){};
@Before("logginOperation()")
public void logBefore(JoinPoint jp){
String name=jp.getSignature().getName();
System.out.println("前置通知,方法"+name);
}
@AfterReturning(pointcut="logginOperation()",returning="result")
public void logAfterReturning(JoinPoint jp,Object result){
String name=jp.getSignature().getName();
System.out.println("后置通知,方法:"+name+",返回值:"+result);
}
@After("logginOperation()")
public void logAfter(JoinPoint jp){
System.out.println("最终通知");
}
}
这样每次写切入点的时候,就可以使用logginOperation()了