Spring AOP

AOP 前奏

实现简单计算器的加减乘除。

需求1-日志:在程序执行期间追踪正在发生的活动

需求2-验证:希望计算器只能处理正数的运算

问题:

•代码混乱:越来越多的非业务需求(日志和验证等)加入后, 原有的业务方法急剧膨胀.  每个方法在处理核心逻辑的同时还必须兼顾其他多个关注点.

•代码分散: 以日志需求为例, 只是为了满足这个单一需求, 就不得不在多个模块(方法)里多次重复相同的日志代码. 如果日志需求发生变化, 必须修改所有模块.

使用动态代理解决:

•代理设计模式的原理: 使用一个代理将对象包装起来, 然后用该代理对象取代原始对象. 任何对原始对象的调用都要通过代理. 代理对象决定是否以及何时将方法调用转到原始对象上.AOP 简介

 1 public class ArithmeticCalculatorImpl implements ArithmeticCalculator {
 2 
 3     @Override
 4     public int add(int i, int j) {
 5         int result = i + j;
 6         return result;
 7     }
 8 
 9     @Override
10     public int sub(int i, int j) {
11         int result = i - j;
12         return result;
13     }
14 
15     @Override
16     public int mul(int i, int j) {
17         int result = i * j;
18         return result;
19     }
20 
21     @Override
22     public int div(int i, int j) {
23         int result = i / j;
24         return result;
25     }
26 
27 }
ArithmeticCalculatorImpl
 1 public class ArithmeticCalculatorLoggingProxy {
 2     
 3     //要代理的对象
 4     private ArithmeticCalculator target;
 5     
 6     public ArithmeticCalculatorLoggingProxy(ArithmeticCalculator target) {
 7         super();
 8         this.target = target;
 9     }
10 
11     public ArithmeticCalculator getLoggingProxy(){
12         ArithmeticCalculator proxy = null;
13         
14         ClassLoader loader = target.getClass().getClassLoader();
15         Class [] interfaces = new Class[]{ArithmeticCalculator.class};
16         InvocationHandler h = new InvocationHandler() {
17             @Override
18             public Object invoke(Object proxy, Method method, Object[] args)
19                     throws Throwable {
20                 String methodName = method.getName();
21                 System.out.println("[before] The method " + methodName + " begins with " + Arrays.asList(args));
22                 Object result = null;    
23                 try {
24                     result = method.invoke(target, args);
25                 } catch (NullPointerException e) {
26                     e.printStackTrace();
27                 }
28                 System.out.println("[after] The method ends with " + result);
29                 
30                 return result;
31             }
32         };
33         
34         proxy = (ArithmeticCalculator) Proxy.newProxyInstance(loader, interfaces, h);
35         
36         return proxy;
37     }
38 }
ArithmeticCalculatorLoggingProxy

AOP 简介

•AOP(Aspect-Oriented Programming, 面向切面编程): 是一种新的方法论, 是对传统 OOP(Object-Oriented Programming, 面向对象编程) 的补充.
•AOP 的主要编程对象是切面(aspect), 而切面模块化横切关注点.
•在应用 AOP 编程时, 仍然需要定义公共功能, 但可以明确的定义这个功能在哪里, 以什么方式应用, 并且不必修改受影响的类. 这样一来横切关注点就被模块化到特殊的对象(切面)里.
•AOP 的好处:
  –每个事物逻辑位于一个位置, 代码不分散, 便于维护和升级
  –业务模块更简洁, 只包含核心业务代码.

AOP 术语

•切面(Aspect):  横切关注点(跨越应用程序多个模块的功能)被模块化的特殊对象
•通知(Advice):  切面必须要完成的工作
•目标(Target): 被通知的对象
•代理(Proxy): 向目标对象应用通知之后创建的对象

•连接点(Joinpoint):程序执行的某个特定位置:如类某个方法调用前、调用后、方法抛出异常后等。连接点由两个信息确定:方法表示的程序执行点;相对点表示的方位。例如 ArithmethicCalculator#add() 方法执行前的连接点,执行点为 ArithmethicCalculator#add(); 方位为该方法执行前的位置

•切点(pointcut):每个类都拥有多个连接点:例如 ArithmethicCalculator 的所有方法实际上都是连接点,即连接点是程序类中客观存在的事务AOP 通过切点定位到特定的连接点。类比:连接点相当于数据库中的记录,切点相当于查询条件。切点和连接点不是一对一的关系,一个切点匹配多个连接点,切点通过 org.springframework.aop.Pointcut 接口进行描述,它使用类和方法作为连接点的查询条件。

Spring  AOP

•AspectJ:Java 社区里最完整最流行的 AOP 框架.
•在 Spring2.0 以上版本中, 可以使用基于 AspectJ 注解或基于 XML 配置的 AOP

在 Spring 中启用 AspectJ 注解支持

•要在 Spring 应用中使用 AspectJ 注解, 必须在 classpath 下包含 AspectJ 类库: aopalliance.jar、aspectj.weaver.jar 和 spring-aspects.jar
将 aop Schema 添加到 <beans> 根元素中.
•要在 Spring IOC 容器中启用 AspectJ 注解支持, 只要在 Bean 配置文件中定义一个空的 XML 元素 <aop:aspectj-autoproxy>
•当 Spring IOC 容器侦测到 Bean 配置文件中的 <aop:aspectj-autoproxy> 元素时, 会自动为与 AspectJ 切面匹配的 Bean 创建代理.
	<!-- 配置自动扫描的包 -->
	<context:component-scan base-package="com.atguigu.spring.aop"></context:component-scan>
	
	<!-- 配置自动为匹配 aspectJ 注解的 Java 类生成代理对象 -->
	<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

用 AspectJ 注解声明切面

要在 Spring 中声明 AspectJ 切面, 只需要在 IOC 容器中将切面声明为 Bean 实例. 当在 Spring IOC 容器中初始化 AspectJ 切面之后, Spring IOC 容器就会为那些与 AspectJ 切面相匹配的 Bean 创建代理.
在 AspectJ 注解中, 切面只是一个带有 @Aspect 注解的 Java 类.
通知是标注有某种注解的简单的 Java 方法.
•AspectJ 支持 5 种类型的通知注解:
  –@Before: 前置通知, 在方法执行之前执行
  –@After: 后置通知, 在方法执行之后执行
  –@AfterRunning: 返回通知, 在方法返回结果之后执行
  –@AfterThrowing: 异常通知, 在方法抛出异常之后
  –@Around: 环绕通知, 围绕着方法执行

利用方法签名编写 AspectJ 切入点表达式

•最典型的切入点表达式时根据方法的签名来匹配各种方法:

execution * com.atguigu.spring.ArithmeticCalculator.*(..): 匹配 ArithmeticCalculator 中声明的所有方法,第一个 * 代表任意修饰符及任意返回值. 
    第二个 * 代表任意方法. .. 匹配任意数量的参数. 若目标类与接口与该切面在同一个包中, 可以省略包名.
execution public * ArithmeticCalculator.*(..): 匹配 ArithmeticCalculator 接口的所有公有方法.
execution public double ArithmeticCalculator.*(..): 匹配 ArithmeticCalculator 中返回 double 类型数值的方法
execution public double ArithmeticCalculator.*(double, ..): 匹配第一个参数为 double 类型的方法, .. 匹配任意数量任意类型的参数
execution public double ArithmeticCalculator.*(double, double): 匹配参数类型为 double, double 类型的方法.

合并切入点表达式

•在 AspectJ 中, 切入点表达式可以通过操作符 &&, ||, ! 结合起来.

让通知访问当前连接点的细节

•可以在通知方法中声明一个类型为 JoinPoint 的参数. 然后就能访问链接细节. 如方法名称和参数值.

后置通知

•后置通知是在连接点完成之后执行的, 即连接点返回结果或者抛出异常的时候, 下面的后置通知记录了方法的终止.

•一个切面可以包括一个或者多个通知.

返回通知

无论连接点是正常返回还是抛出异常, 后置通知都会执行. 如果只想在连接点返回的时候记录日志, 应使用返回通知代替后置通知.

•在返回通知中, 只要将 returning 属性添加到 @AfterReturning 注解中, 就可以访问连接点的返回值. 该属性的值即为用来传入返回值的参数名称.

•必须在通知方法的签名中添加一个同名参数. 在运行时, Spring AOP 会通过这个参数传递返回值.

原始的切点表达式需要出现在 pointcut 属性中

异常通知

•只在连接点抛出异常时才执行异常通知

将 throwing 属性添加到 @AfterThrowing 注解中, 也可以访问连接点抛出的异常. Throwable 是所有错误和异常类的超类. 所以在异常通知方法可以捕获到任何错误和异常.

如果只对某种特殊的异常类型感兴趣, 可以将参数声明为其他异常的参数类型. 然后通知就只在抛出这个类型及其子类的异常时才被执行.

环绕通知

•环绕通知是所有通知类型中功能最为强大的, 能够全面地控制连接点. 甚至可以控制是否执行连接点.

对于环绕通知来说, 连接点的参数类型必须是 ProceedingJoinPoint . 它是 JoinPoint 的子接口, 允许控制何时执行, 是否执行连接点.

在环绕通知中需要明确调用 ProceedingJoinPoint 的 proceed() 方法来执行被代理的方法. 如果忘记这样做就会导致通知被执行了, 但目标方法没有被执行.

•注意: 环绕通知的方法需要返回目标方法执行之后的结果, 即调用 joinPoint.proceed(); 的返回值, 否则会出现空指针异常

 1 /**
 2  * 可以使用 @Order 注解指定切面的优先级, 值越小优先级越高
 3  */
 4 @Order(2)
 5 @Aspect
 6 @Component
 7 public class LoggingAspect {
 8     
 9     /**
10      * 定义一个方法, 用于声明切入点表达式. 一般地, 该方法中再不需要添入其他的代码. 
11      * 使用 @Pointcut 来声明切入点表达式. 
12      * 后面的其他通知直接使用方法名来引用当前的切入点表达式. 
13      */
14     @Pointcut("execution(public int com.atguigu.spring.aop.ArithmeticCalculator.*(..))")
15     public void declareJointPointExpression(){}
16     
17     /**
18      * 在 com.atguigu.spring.aop.ArithmeticCalculator 接口的每一个实现类的每一个方法开始之前执行一段代码
19      */
20     @Before("declareJointPointExpression()")
21     public void beforeMethod(JoinPoint joinPoint){
22         String methodName = joinPoint.getSignature().getName();
23         Object [] args = joinPoint.getArgs();
24         
25         System.out.println("The method " + methodName + " begins with " + Arrays.asList(args));
26     }
27     
28     /**
29      * 在方法执行之后执行的代码. 无论该方法是否出现异常
30      */
31     @After("declareJointPointExpression()")
32     public void afterMethod(JoinPoint joinPoint){
33         String methodName = joinPoint.getSignature().getName();
34         System.out.println("The method " + methodName + " ends");
35     }
36     
37     /**
38      * 在方法法正常结束受执行的代码
39      * 返回通知是可以访问到方法的返回值的!
40      */
41     @AfterReturning(value="declareJointPointExpression()",
42             returning="result")
43     public void afterReturning(JoinPoint joinPoint, Object result){
44         String methodName = joinPoint.getSignature().getName();
45         System.out.println("The method " + methodName + " ends with " + result);
46     }
47     
48     /**
49      * 在目标方法出现异常时会执行的代码.
50      * 可以访问到异常对象; 且可以指定在出现特定异常时在执行通知代码
51      */
52     @AfterThrowing(value="declareJointPointExpression()",
53             throwing="e")
54     public void afterThrowing(JoinPoint joinPoint, Exception e){
55         String methodName = joinPoint.getSignature().getName();
56         System.out.println("The method " + methodName + " occurs excetion:" + e);
57     }
58     
59     /**
60      * 环绕通知需要携带 ProceedingJoinPoint 类型的参数. 
61      * 环绕通知类似于动态代理的全过程: ProceedingJoinPoint 类型的参数可以决定是否执行目标方法.
62      * 且环绕通知必须有返回值, 返回值即为目标方法的返回值
63      */
64     /*
65     @Around("execution(public int com.atguigu.spring.aop.ArithmeticCalculator.*(..))")
66     public Object aroundMethod(ProceedingJoinPoint pjd){
67         
68         Object result = null;
69         String methodName = pjd.getSignature().getName();
70         
71         try {
72             //前置通知
73             System.out.println("The method " + methodName + " begins with " + Arrays.asList(pjd.getArgs()));
74             //执行目标方法
75             result = pjd.proceed();
76             //返回通知
77             System.out.println("The method " + methodName + " ends with " + result);
78         } catch (Throwable e) {
79             //异常通知
80             System.out.println("The method " + methodName + " occurs exception:" + e);
81             throw new RuntimeException(e);
82         }
83         //后置通知
84         System.out.println("The method " + methodName + " ends");
85         
86         return result;
87     }
88     */
89 }
LoggingAspect

 指定切面的优先级

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

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

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

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

重用切入点定义

•在编写 AspectJ 切面时, 可以直接在通知注解中书写切入点表达式. 但同一个切点表达式可能会在多个通知中重复出现.

•在 AspectJ 切面中, 可以通过 @Pointcut 注解将一个切入点声明成简单的方法. 切入点的方法体通常是空的, 因为将切入点定义与应用程序逻辑混在一起是不合理的.

切入点方法的访问控制符同时也控制着这个切入点的可见性. 如果切入点要在多个切面中共用, 最好将它们集中在一个公共的类中. 在这种情况下, 它们必须被声明为 public. 在引入这个切入点时, 必须将类名也包括在内. 如果类没有与这个切面放在同一个包中, 还必须包含包名.

•其他通知可以通过方法名称引入该切入点.

用基于 XML 的配置声明切面

•除了使用 AspectJ 注解声明切面, Spring 也支持在 Bean 配置文件中声明切面. 这种声明是通过 aop schema 中的 XML 元素完成的.
•正常情况下, 基于注解的声明要优先于基于 XML 的声明. 通过 AspectJ 注解, 切面可以与 AspectJ 兼容, 而基于 XML 的配置则是 Spring 专有的. 由于 AspectJ 得到越来越多的 AOP 框架支持, 所以以注解风格编写的切面将会有更多重用的机会.
 1     <!-- 配置 bean -->
 2     <bean id="arithmeticCalculator" 
 3         class="com.atguigu.spring.aop.xml.ArithmeticCalculatorImpl"></bean>
 4 
 5     <!-- 配置切面的 bean. -->
 6     <bean id="loggingAspect"
 7         class="com.atguigu.spring.aop.xml.LoggingAspect"></bean>
 8 
 9     <bean id="vlidationAspect"
10         class="com.atguigu.spring.aop.xml.VlidationAspect"></bean>
11 
12     <!-- 配置 AOP -->
13     <aop:config>
14         <!-- 配置切点表达式 -->
15         <aop:pointcut expression="execution(* com.atguigu.spring.aop.xml.ArithmeticCalculator.*(int, int))" 
16             id="pointcut"/>
17         <!-- 配置切面及通知 -->
18         <aop:aspect ref="loggingAspect" order="2">
19             <aop:before method="beforeMethod" pointcut-ref="pointcut"/>
20             <aop:after method="afterMethod" pointcut-ref="pointcut"/>
21             <aop:after-throwing method="afterThrowing" pointcut-ref="pointcut" throwing="e"/>
22             <aop:after-returning method="afterReturning" pointcut-ref="pointcut" returning="result"/>
23             <!--  
24             <aop:around method="aroundMethod" pointcut-ref="pointcut"/>
25             -->
26         </aop:aspect>    
27         <aop:aspect ref="vlidationAspect" order="1">
28             <aop:before method="validateArgs" pointcut-ref="pointcut"/>
29         </aop:aspect>
30     </aop:config>
31 
32 </beans>
applicationContext-xml

 

posted @ 2016-09-03 09:41  岳灵珊  阅读(267)  评论(0编辑  收藏  举报