一起学Spring之AOP

前端之家收集整理的这篇文章主要介绍了一起学Spring之AOP前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

在软件开发中,我们重点关注的是业务逻辑代码,但在实际开发中,需要写的代码却不仅仅是业务逻辑,还需要处理记录日志,异常处理,事务控制等一些与业务无关的事情。而且这些代码也是服务端必须的,类似这样的代码分散在系统中的各个地方,如:几乎所有的重要操作方法前面都会加上日志记录代码,这样的代码写起来繁琐,又占用开发时间和精力,而且不容易维护。我们统一把这类代码成为【切面代码】,如何让我们从这些繁琐的工作中抽身而退,更加专注于业务逻辑,这就需要用到Spring的AOP技术。

AOP原理:将复杂的需求分解成不同的方面,将散落在系统中的公共功能集中解决,如下图所示:

通知(Advice)的分类

分类如下:

准备工作

AOP需要的jar包

除Spring必备的五个jar包外,还需要以下三个来支撑AOP:

  • aopalliance-1.0.jar
  • aspectjweaver-1.5.3.jar
  • spring-aop-4.0.6.RELEASE.jar

定义一个接口和实现类

如下所示:

IStudentService接口 代码如下:

  1. 1 package com.hex.second;
  2. 2
  3. 3 /**
  4. 4 * 学生服务接口
  5. 5 * @author Administrator
  6. 6 *
  7. 7 */
  8. 8 public interface IStudentService {
  9. 9
  10. 10 11 * 新增学生
  11. 12 * @param student
  12. 13 14 void addStudent(Student student);
  13. 15 16 * 删除学生
  14. 17 id
  15. 18 19 void deleteStudent(int id);
  16. 20
  17. 21 22 * 修改学生
  18. 23 24 25 void updateStudent(26 }
View Code

StudentServiceImpl类 代码如下:

  1. * 学生服务事项类
  2. class StudentServiceImpl implements 9
  3. 12 13 addStudent(Student student) {
  4. 14 // TODO Auto-generated method stub
  5. 15 System.out.println("新增加学生。。。");
  6. }
  7. 17
  8. 18 19 20 21 @Override
  9. 22 id) {
  10. 23 24 System.out.println("删除学生。。。"25 26
  11. 27 28 29 30 31 32 System.out.println("修改学生"33 int i=1/0;
  12. 34 35 }
View Code

前置通知

1. 实现接口

前置通知类,需要实现【MethodBeforeAdvice】接口中的before方法,如下所示:

Method method 表示执行的目标方法

Object[] args 表示传入的参数数组

Object target 表示目标对象,即切入点所示的对象

  1. import java.lang.reflect.Method;
  2. 4
  3. org.springframework.aop.MethodBeforeAdvice;
  4. 6
  5. 7 class LogBefore MethodBeforeAdvice {
  6. 8
  7. 9 /***
  8. 10 * 前置通知
  9. * method:表示调用方法,即切入点
  10. * args:表示调用方法的参数
  11. 13 * target:表示方法所在的目标对象
  12. 14 15 16 void before(Method method,Object[] args,Object target) throws Throwable {
  13. 17 18 System.out.println("前置通知。。。"19 System.out.println("method="+method+",args数量="+args.length+",target="+target);
  14. 20 21 }

2. 配置applicationContext.xml文件

如果要支持AOP,需要引入命名空间,如下所示:

  1. 1 <?xml version="1.0" encoding="UTF-8"?>
  2. 2 <beans xmlns="http://www.springframework.org/schema/beans"
  3. 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. 4 xmlns:p="http://www.springframework.org/schema/p"
  5. 5 xmlns:aop="http://www.springframework.org/schema/aop"
  6. 6 xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. 7 http://www.springframework.org/schema/beans/spring-beans.xsd
  8. 8 http://www.springframework.org/schema/aop
  9. 9 http://www.springframework.org/schema/aop/spring-aop.xsd">

3. 配置两个类对应的bean

  1. <!-- 服务类 -->
  2. bean id="studentService" class="com.hex.second.StudentServiceImpl"></bean>
  3. 3 前置通知4 ="logBefore"="com.hex.second.LogBefore"4. 配置AOP
  4. 通过AOP配置,将通知类和业务逻辑类进行关联,说明如下:

  5. 一个配置文件中,可以有多个<aop:config>配置,每一个aop:config中只能有一个aop:pointcut配置,如果有多个切入点需要配置expression,且切入点必须是全路径配置。如下所示:

  6.  将addStudent和通知进行关联 aop:config>
  7.       每一个config只有一个poingcut,如果有多个,则需要配置多个config -->
  8.       配置切入点  id自定义,expression表示切入点的函数aop:pointcut expression="execution(public void com.hex.second.StudentServiceImpl.addStudent(com.hex.second.Student))" id="pc"/>
  9.       配置通知 aop:advisor advice-ref pointcut-ref/>
  10. </>
  11.  
  12. 后置通知

  13. 1. 实现接口

  14. 需要实现【AfterReturningAdvice】接口【afterReturning】方法中的 如下所示:

  15.  org.springframework.aop.AfterReturningAdvice;
  16.  * 通过实现接口将普通类变成后置通知
  17.  9 11  class LogAfter  AfterReturningAdvice {
  18. 13 
  19.      * 后置通知实现类
  20.      * returnValue:返回值
  21. 18 void afterReturning(Object returnValue,Method method,1)">24         System.out.println("后置通知。。。"25         System.out.println("returnValue="+returnValue+",method="+method+",1)">26 27 
  22. 28 }
  23.  
  24. 2. 配置切入点和通知的Bean

  25. ="logAfter"="com.hex.second.LogAfter"3. AOP配置
  26. 如果前置通知和后置通知为同一个切入点,则可以配置在一个aop:config节点中,如下所示:

  27. 多个切入点用or连接,多个通知就配置多个aop:advisor

  28.  2  3       4       5      ="execution(public void com.hex.second.StudentServiceImpl.deleteStudent(int)) or execution(public void com.hex.second.StudentServiceImpl.addStudent(com.hex.second.Student))" 6       7       8          
  29.  9      异常通知
  30. 1. 实现接口

  31. 异常通知是有异常发生时,才会触发的通知,需要实现【ThrowsAdvice】接口,且此接口没有需要实现的方法,但同时给出了约定:

  32. 必须以固定格式实现方法:public void afterThrowing([Method,args,target],ThrowableSubclass);

  33.  org.springframework.aop.ThrowsAdvice;
  34.  * 异常通知
  35. class LogException  ThrowsAdvice {
  36. 13     
  37.      * 异常通知执行
  38.  method 切入点
  39.  args 参数个数
  40.  target 调用目标对象
  41.  ex 异常
  42.  afterThrowing(Method method,Object target,Exception ex){
  43. 22         System.out.println("异常通知。。。"23         System.out.println("method="+method+",target="+target+",ex="+ex);
  44. 24 25 }
  45. 2. 配置Bean类

  46. ="logException"="com.hex.second.LogException"3. 配置AOP
  47. 如下所示:参数只需要写参数类型即可,不需要写参数名称

  48.  可以配置aop:config 3         ="execution(public void com.hex.second.StudentServiceImpl.updateStudent(int))"="pc1"4      5      6 环绕通知
  49. 1. 实现接口

  50. 环绕通知,需要实现【MethodInterceptor】接口并实现【invoke】方法,其中obj = invocation.proceed();表示调用目标方法,如果不写此句,则目标方法不会被调用。如下所示:

  51.  org.aopalliance.intercept.MethodInterceptor;
  52.  org.aopalliance.intercept.MethodInvocation;
  53.  5 
  54.  * 环绕通知
  55.  * 环绕通知的本质上是一个拦截
  56. class LogAround  MethodInterceptor {
  57.      * 
  58. 16      public Object invoke(MethodInvocation invocation) 19         Object obj = null20         try {
  59. 21 
  60. 22              前置通知
  61. 23             System.out.println("环绕实现前置通知。。。"24             System.out.println("环绕通知:target="+invocation.getThis()+",method="+invocation.getMethod().getName()+",args="+invocation.getArguments().length);
  62. 25              控制目标方法的执行 obj表示目标方法的返回值,表示执行addStudent(student)方法
  63. 26             方法控制目标方法的执行,如果不写此方法,则目标方法不会执行,此方法前的是前置通知,此方法后的是后置通知
  64. 27             obj = invocation.proceed();
  65. 28              后置通知
  66. 29             System.out.println("环绕实现后置通知。。。"30         } catch (Exception e) {
  67. 31              异常通知
  68. 32             System.out.println("环绕实现异常通知。。。"33             throw e;
  69.         }
  70. 35         36         return obj;
  71. 37 38 
  72. 39 }
  73. 2. 配置Bean

  74. ="logAround"="com.hex.second.LogAround"</bean3. 配置AOP
  75. 所有配置切入点通知的方式都是一样的。如下所示:

  76. 2      ="pc2"3      >
  77. 所有的调用方式是一致的,不需要调用通知类,系统会自动调用,如下所示:

  78.  org.springframework.context.ApplicationContext;
  79.  org.springframework.context.support.ClassPathXmlApplicationContext;
  80. class TestMain {
  81.  7 
  82.  8     static  main(String[] args) {
  83.  9          TODO Auto-generated method stub
  84. 10         通过Spring进行注入,Spring上下文对象
  85. 11         ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"12         IStudentService studentService=(IStudentService)context.getBean("studentService"13         Student student =new Student();
  86. 14         studentService.addStudent(student);
  87. 15         studentService.deleteStudent(1);
  88. 16         studentService.updateStudent(0);
  89. 18 
  90. 19 }
  91. View Code
  92.  

  93. 备注

  94. 合抱之木,生于毫末;九层之台,起于累土;千里之行,始于足下。

  95.       
  96.       
  97.                  
    •               
    •             
    •           
    •           
    •           
    •           
    •           
    • 猜你在找的Spring相关文章

    •