[原创]java WEB学习笔记107:Spring学习---AOP切面的优先级,重用切点表达式

本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用

内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系。

本人互联网技术爱好者,互联网技术发烧友

微博:伊直都在0221

QQ:951226918

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

1.切面的优先级

  1) 在同一个连接点上应用不止一个切面时, 除非明确指定, 否则它们的优先级是不确定的.

  2) 切面的优先级可以通过实现 Ordered 接口或利用 @Order 注解指定.

  3) 实现 Ordered 接口, getOrder() 方法的返回值越小, 优先级越高.

  4) 若使用 @Order 注解, 序号出现在注解中

ValidateArgs.java
 1 package com.jason.spring.aop.impl;
 2 
 3 import java.util.Arrays;
 4 
 5 import org.aspectj.lang.JoinPoint;
 6 import org.aspectj.lang.annotation.Aspect;
 7 import org.aspectj.lang.annotation.Before;
 8 import org.springframework.context.annotation.Bean;
 9 import org.springframework.core.annotation.Order;
10 import org.springframework.stereotype.Component;
11 
12 /**
13  * 
14  * @ClassName:ValidateArgs
15  * @Description:可以使用@Order 注解指定切面的优先级,值越小优先级越高
16  * @author: jason_zhangz@163.com
17  * @date:2016年12月6日下午2:14:55
18  * 
19  * 
20  *
21  */
22 @Order(2)
23 @Component
24 @Aspect
25 public class ValidateArgs {
26     
27     @Before("execution(* com.jason.spring.aop.impl.ArithmeticCaculator.*(..))")
28     public void validateArgs(JoinPoint joinPoint){
29         System.out.println("validate:" + Arrays.asList(joinPoint.getArgs()));
30     }
31 
32 }

 

LoggingAspect.java 
 1 package com.jason.spring.aop.impl;
 2 
 3 import java.util.Arrays;
 4 import java.util.List;
 5 
 6 import org.aspectj.lang.JoinPoint;
 7 import org.aspectj.lang.ProceedingJoinPoint;
 8 import org.aspectj.lang.annotation.After;
 9 import org.aspectj.lang.annotation.AfterReturning;
10 import org.aspectj.lang.annotation.AfterThrowing;
11 import org.aspectj.lang.annotation.Around;
12 import org.aspectj.lang.annotation.Aspect;
13 import org.aspectj.lang.annotation.Before;
14 import org.aspectj.lang.annotation.Pointcut;
15 import org.springframework.core.annotation.Order;
16 import org.springframework.stereotype.Component;
17 
18 
19 @Order(1)
20 //把这个类声明为一个切面
21 //1.需要将该类放入到IOC 容器中
22 @Component
23 //2.再声明为一个切面
24 @Aspect
25 public class LoggingAspect {
26     
27     /**
28      * 
29      * @Author:jason_zhangz@163.com
30      * @Title: declareJointPointExpression 
31      * @Time:2016年12月6日
32      * @Description: 定义一个方法,用于声明切入点表达式。一般的,该方法不需要添加其他代码
33      *
34      */
35     @Pointcut("execution(* com.jason.spring.aop.impl.*.*(int, int))")
36     public void declareJointPointExpression(){}
37     
38     
39     //声明该方法是一个前置通知:在目标方法开始之前执行 哪些类,哪些方法
40     //作用:@before 当调用目标方法,而目标方法与注解声明的方法相匹配的时候,aop框架会自动的为那个方法所在的类生成一个代理对象,在目标方法执行之前,执行注解的方法
41     //支持通配符
42     //@Before("execution(public int com.jason.spring.aop.impl.ArithmeticCaculatorImpl.*(int, int))")
43     @Before("declareJointPointExpression()")
44     public void beforeMethod(JoinPoint joinPoint){
45         String methodName = joinPoint.getSignature().getName();
46         List<Object> args = Arrays.asList(joinPoint.getArgs());
47         System.out.println("The method " + methodName + " begins " + args);
48     }
49 }

 

 

2.重用切入点定义

    

 

posted @ 2016-12-06 14:31  jason_zhangz  阅读(1164)  评论(0编辑  收藏  举报