切面

Spring 中的 Aop 的通知类型有 5 种:

  • 前置通知
  • 后置通知
  • 异常通知
  • 返回通知
  • 环绕通知

首先,在项目中,引入 Spring 依赖(引入 Aop 相关的依赖):

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.1.9.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.9.5</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.9.5</version>
    </dependency>
</dependencies>

接下来,定义切点,这里介绍两种切点的定义方式:

  • 使用自定义注解
  • 使用规则
  • 使用xml

其中,使用自定义注解标记切点,是侵入式的,所以这种方式在实际开发中==不推荐==,仅作为了解,另一种使用规则来定义切点的方式,无侵入,一般推荐使用这种方式。

① 自定义注解

首先自定义一个注解:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Action {
}

然后在需要拦截的方法上,添加该注解,在 add 方法上添加了 @Action 注解,表示该方法将会被 Aop 拦截,而其他未添加该注解的方法则不受影响。

@Component
public class MyCalculatorImpl {
    @Action
    public int add(int a, int b) {
        return a + b;
    }

    public void min(int a, int b) {
        System.out.println(a + "-" + b + "=" + (a - b));
    }
}

接下来,定义增强(通知、Advice),使用统一切点,这里除了使用JoinPoint获取参数,还可以通过args获取,详见这里

@Component
@Aspect//表示这是一个切面
public class LogAspect {

    /**
     * 可以统一定义切点
     */
    @Pointcut("@annotation(Action)")
    public void pointcut() {
    }

    /**
     * @param joinPoint 包含了目标方法的关键信息
     * @Before 注解表示这是一个前置通知,即在目标方法执行之前执行,注解中,需要填入切点
     */
    @Before(value = "pointcut()")
    public void before(JoinPoint joinPoint) {
        Signature signature = joinPoint.getSignature();
        String name = signature.getName();
        System.out.println(name + "方法开始执行了...");
    }

    /**
     * 后置通知
     * @param joinPoint 包含了目标方法的所有关键信息
     * @After 表示这是一个后置通知,即在目标方法执行之后执行
     */
    @After("pointcut()")
    public void after(JoinPoint joinPoint) {
        Signature signature = joinPoint.getSignature();
        String name = signature.getName();
        System.out.println(name + "方法执行结束了...");
    }

    /**
     * @param joinPoint
     * @AfterReturning 表示这是一个返回通知,即有目标方法有返回值的时候才会触发,该注解中的 returning 属性表示目标方法返回值的变量名,这个需要和参数一一对应,注意:目标方法的返回值类型要和这里方法返回值参数的类型一致,否则拦截不到,如果想拦截所有(包括返回值为 void),则方法返回值参数可以为 Object
     */
    @AfterReturning(value = "pointcut()",returning = "r")
    public void returing(JoinPoint joinPoint,Integer r) {
        Signature signature = joinPoint.getSignature();
        String name = signature.getName();
        System.out.println(name + "方法返回:"+r);
    }

    /**
     * 异常通知
     * @param joinPoint
     * @param e 目标方法所抛出的异常,注意,这个参数必须是目标方法所抛出的异常或者所抛出的异常的父类,只有这样,才会捕获。如果想拦截所有,参数类型声明为 Exception
     */
    @AfterThrowing(value = "pointcut()",throwing = "e")
    public void afterThrowing(JoinPoint joinPoint,Exception e) {
        Signature signature = joinPoint.getSignature();
        String name = signature.getName();
        System.out.println(name + "方法抛异常了:"+e.getMessage());
    }

    /**
     * 环绕通知
     *
     * 环绕通知是集大成者,可以用环绕通知实现上面的四个通知,这个方法的核心有点类似于在这里通过反射执行方法
     * @param pjp
     * @return 注意这里的返回值类型最好是 Object ,和拦截到的方法相匹配
     */
    @Around("pointcut()")
    public Object around(ProceedingJoinPoint pjp) {
        Object proceed = null;
        try {
            //这个相当于 method.invoke 方法,我们可以在这个方法的前后分别添加日志,就相当于是前置/后置通知
            proceed = pjp.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return proceed;
    }
}

通知定义完成后,接下来在配置类中,开启包扫描和自动代理:

@Configuration
@ComponentScan
@EnableAspectJAutoProxy//开启自动代理
public class JavaConfig {
}

然后,在 main 方法中,开启调用:

public class Main {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(JavaConfig.class);
        MyCalculatorImpl myCalculator = ctx.getBean(MyCalculatorImpl.class);
        myCalculator.add(3, 4);
        myCalculator.min(3, 4);
    }
}

② 使用自定义规则

使用注解是侵入式的,可以继续优化,改为非侵入式的。重新定义切点,新切点的定义就不再需要 @Action 注解了,改为更为通用的拦截方式:

@Component
@Aspect//表示这是一个切面
public class LogAspect {

    /**
     * 可以统一定义切点
     * 第一个 * 表示要拦截的目标方法返回值任意(也可以明确指定返回值类型)
     * 第二个 * 表示包中的任意类(也可以明确指定类)
     * 第三个 * 表示类中的任意方法
     * 最后面的两个点表示方法参数任意,个数任意,类型任意
     */
    @Pointcut("execution(* org.javaboy.aop.commons.*.*(..))")
    public void pointcut() {
    }
/*
	后面直接 value = "pointcut()"
*/
}
③ 使用xml配置

定义通知/增强,单纯定义行为,无需注解:

public class LogAspect {

    public void before(JoinPoint joinPoint) {
        Signature signature = joinPoint.getSignature();
        String name = signature.getName();
        System.out.println(name + "方法开始执行了...");
    }
    
    public void after(JoinPoint joinPoint) {
        Signature signature = joinPoint.getSignature();
        String name = signature.getName();
        System.out.println(name + "方法执行结束了...");
    }

    public void returing(JoinPoint joinPoint,Integer r) {
        Signature signature = joinPoint.getSignature();
        String name = signature.getName();
        System.out.println(name + "方法返回:"+r);
    }
    
    public void afterThrowing(JoinPoint joinPoint,Exception e) {
        Signature signature = joinPoint.getSignature();
        String name = signature.getName();
        System.out.println(name + "方法抛异常了:"+e.getMessage());
    }
    
    public Object around(ProceedingJoinPoint pjp) {
        Object proceed = null;
        try {
            //这个相当于 method.invoke 方法,我们可以在这个方法的前后分别添加日志,就相当于是前置/后置通知
            proceed = pjp.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return proceed;
    }
}

接下来在 xml中配置 Aop:

<bean class="org.javaboy.aop.LogAspect" id="logAspect"/>
<aop:config>
    <aop:pointcut id="pc1" expression="execution(* org.javaboy.aop.commons.*.*(..))"/>
    <aop:aspect ref="logAspect">
        <aop:before method="before" pointcut-ref="pc1"/>
        <aop:after method="after" pointcut-ref="pc1"/>
        <aop:after-returning method="returing" pointcut-ref="pc1" returning="r"/>
        <aop:after-throwing method="afterThrowing" pointcut-ref="pc1" throwing="e"/>
        <aop:around method="around" pointcut-ref="pc1"/>
    </aop:aspect>
</aop:config>

最后,在 主方法中加载配置文件:

public class Main {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        MyCalculatorImpl myCalculator = ctx.getBean(MyCalculatorImpl.class);
        myCalculator.add(3, 4);
        myCalculator.min(5, 6);
    }
}

其他

对外部引用类进行增强

假定已有外部类StudentServiceImpl,其接口为StudentService,现要对其进行增强

//定义接口
public interface StuValidator{
    //检测对象是否为空
    public boolean validate(Student stu);
}
//实现类
public class StuValidatorImpl implements StuValidator{
    @Override
    //判断stu是否为空
    public boolean validate(Student stu){
        return stu!=null;
    }
}

然后在切面类中引入接口:

  • @DeclareParents:引入新的类来增强服务
    • value:要增强的目标对象
    • defaultImpl:引入增强功能的类
@Aspect
public class MyAspect{
    @DeclareParents(
    value="com.xu.StudentServiceImpl+",
    defaultImpl=StuValidatorImpl.class
    )
    public StuValidator stuValidator;
}

使用时:

@Autowired
StudentService studentService;
//强制转换
StuValidator stuValidator=(StuValidator) studentService;
if(stuValidator.validator(stu)){
    studentService.printStu(stu);
}

实际通过动态代理,newProxyInstance传入的第二个参数,为对象数组,把StudentServiceStuValidator都传入,使代理对象挂到两个接口下,能相互转换并使用方法

上当了,有什么用。。

通知获取参数

args(user)把连接点中名称为 user 的参数传递进来,也可以通过point.getArgs()获取所有参数

@Before("pointCut()&&args(user)")
public void before(JointPoint point, User user){
    Object[] args=point.getArgs();
    sout("before..");
}

织入方法

Spring 允许的动态代理方式有两种,一种是JDK动态代理,还有一种是CGLIB

其中JDK动态代理要求被代理的目标对象必须==有接口==(即为接口的实现类),当被代理的对象没有接口时,会以CGLIB运行

多个切面

使用多个切面运行时,通过在切面类上添加@Order(num)(1,2,3…)设置切面运行顺序