Aspect Oriented Programing 面向切面编程,相比较 oop 面向对象编程来说,Aop 关注的不再是程序代码中某个类,某些方法,而 aop 考虑的更多的是一种面到面的切入,即层与层之间的一种切入,所以称之为切面。联想大家吃的汉堡(中间夹肉)。那么 aop 是怎么做到拦截整个面的功能呢?考虑学到的 servlet urlpattern /* 的配置,实际上也是 aop 的实现 。

Spring Aop 实现的方式

注解 方式

XML 方式

案例实操

注解方式

jar 包坐标引入

`<dependency>
   <groupId>org.aspectj</groupId>
   <artifactId>aspectjweaver</artifactId>
   <version>1.8.9</version>
</dependency>` 

*   1
*   2
*   3
*   4
*   5

beans.xml 配置

添加命名空间

`xmlns:aop="http://www.springframework.org/schema/aop"http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop.xsd` 

*   1
*   2

配置 Aop 代理

`<aop:aspectj-autoproxy/>` 

*   1

编写 aop 实现类

`/**
* 声明切面组件
*/
@Component
@Aspect
public class LogCut {
   /**
   * 定义切入点 匹配方法规则定义
   * 匹配规则表达式含义 拦截 com.xxx.service 包下 以及子包下 所有类的所有方法
   */
@Pointcut("execution (* com.xxx.service..*.*(..))")
public void cut(){}
   /**
   * 声明前置通知 并将通知应用到定义的切入点上
   * 目标类方法执行前 执行该通知
   */
   @Before(value="cut()")
   public void before(){
     System.out.println("前置通知.....");
   }
   /**
   * 声明返回通知 并将通知应用到切入点上
   * 目标类方法执行完毕执行该通知
   */
   @AfterReturning(value="cut()")
   public void afterReturning(){
    System.out.println("返回通知....");
   }
   /**
   * 声明最终通知 并将通知应用到切入点上
   * 目标类方法执行过程中是否发生异常 均会执行该通知 相当于异常中的 finally 
   */
   @After(value="cut()")
   public void after(){
    System.out.println("最终通知....");
   }
   /**
   * 声明异常通知 并将通知应用到切入点上
   * 目标类方法执行时发生异常 执行该通知
   */
   @AfterThrowing(value="cut()",throwing="e")
   public void afterThrowing(Exception e){
    System.out.println("异常通知....方法执行异常时执行:"+e);
   }
   /**
   * 声明环绕通知 并将通知应用到切入点上
   * 方法执行前后 通过环绕通知定义相应处理
   */
   @Around(value="cut()")
   public Object around(ProceedingJoinPoint pjp) throws Throwable{
       System.out.println("环绕前置...");
       System.out.println("环绕通知");
       System.out.println(pjp.getTarget()+"--"+pjp.getSignature());
       Object result=pjp.proceed();//执行目标对象方法
       System.out.println("环绕后置...");
       return result;
   } 
}` 

*   1
*   2
*   3
*   4
*   5
*   6
*   7
*   8
*   9
*   10
*   11
*   12
*   13
*   14
*   15
*   16
*   17
*   18
*   19
*   20
*   21
*   22
*   23
*   24
*   25
*   26
*   27
*   28
*   29
*   30
*   31
*   32
*   33
*   34
*   35
*   36
*   37
*   38
*   39
*   40
*   41
*   42
*   43
*   44
*   45
*   46
*   47
*   48
*   49
*   50
*   51
*   52
*   53
*   54
*   55
*   56
*   57
*   58

Aop 匹配方法规则表达式语言(简要了解)

Aop 切入点表达式简介

执行任意公共方法:

`execution(public *(..))

执行任意的 set 方法

execution(* set*(..))

执行 com.xxx.service 包下任意类的任意方法

execution(* com.xxx.service.*.*(..))

执行 com.xxx.service 包 以及子包下任意类的任意方法

execution(* com.xxx.service..*.*(..))` 

*   1
*   2
*   3
*   4
*   5
*   6
*   7
*   8
*   9
*   10
*   11
*   12
*   13

xml 方式

配置切面、切入点、通知

`<!-- aop 相关配置 -->
<aop:config>
   <!-- aop 切面配置 -->
   <aop:aspect ref="logCut">
       <!-- 定义 aop 切入点 -->
       <aop:pointcut expression="execution (* com.xxx.service..*.*(..))" 
       id="cut"/>
       <!-- 配置前置通知 指定前置通知方法名 并引用切入点定义 -->
       <aop:before method="before" pointcut-ref="cut"/>
       <!-- 配置返回通知 指定返回通知方法名 并引用切入点定义 -->
       <aop:after-returning method="afterReturning" pointcut-ref="cut"/>
       <!-- 配置异常通知 指定异常通知方法名 并引用切入点定义 -->
       <aop:after-throwing method="afterThrowing" throwing="e" pointcut-ref="cut"/>
       <!-- 配置最终通知 指定最终通知方法名 并引用切入点定义 -->
       <aop:after method="after" pointcut-ref="cut"/>
       <!-- 配置环绕通知 指定环绕通知方法名 并引用切入点定义 -->
       <aop:around method="around" pointcut-ref="cut"/>
</aop:aspect>
</aop:config>` 

*   1
*   2
*   3
*   4
*   5
*   6
*   7
*   8
*   9
*   10
*   11
*   12
*   13
*   14
*   15
*   16
*   17
*   18
*   19

定义 bean

`/**
* 声明切面组件
*/
@Component
public class LogCut {
   public void before(){
    System.out.println("前置通知.....");
   }
   public void afterReturning(){
    System.out.println("返回通知....");
   } 
   public void after(){
    System.out.println("最终通知....");
   }
   public void afterThrowing(Exception e){
    System.out.println("异常通知....方法执行异常时执行:" + e);
   }
   public Object around(ProceedingJoinPoint pjp) throws Throwable{
       System.out.println("环绕前置...");
       System.out.println("环绕通知");
       System.out.println(pjp.getTarget()+"--"+pjp.getSignature());
       Object result=pjp.proceed();
       System.out.println("环绕后置...");
       return result;
   }
}` 

*   1
*   2
*   3
*   4
*   5
*   6
*   7
*   8
*   9
*   10
*   11
*   12
*   13
*   14
*   15
*   16
*   17
*   18
*   19
*   20
*   21
*   22
*   23
*   24
*   25
*   26

扩展

AOP 的基本概念

JoinPoint(连接点)【动态】

被拦截到的每个点,spring 中指被拦截到的每一个方法,spring aop 一个连接点即代表一个方法的执行。

Pointcut(切入点)【静态】

对连接点进行拦截的定义(匹配规则定义 规定拦截哪些方法,对哪些方法进行处理),spring 这块有专门的表达式语言定义。

Advice(通知){重点}

拦截到每一个连接点即(每一个方法)前后所要做的操作

前置通知(前置增强)–before() 执行方法前通知

返回通知(返回增强)–afterReturning 方法正常结束返回后的通知

异常抛出通知(异常抛出增强)–afetrThrow()

最终通知 --after 无论方法是否发生异常,均会执行该通知

环绕通知 --around 包围一个连接点(join point)的通知,如方法调用。这是最强大的一种通知类型。 环绕通知可以在方法调用前后完成自定义的行为。它也会选择是否继续执行连接点或直接返回它们自己的返回值或抛出异常来结束执行

Aspect(切面)

切入点与通知的结合,决定了切面的定义,切入点定义了要拦截哪些类的 哪些方法,通知则定义了拦截方法后要做什么,切面则是横切关注点的抽象,与类相似,类是对物体特征的抽象,切面则是横切关注点抽象。

Target(目标对象)

被代理的目标对象

Weave(织入)

将切面应用到目标对象并生成代理对象的这个过程即为织入(过程)。

Introduction(引入)

在不修改原有应用程序代码的情况下,在程序运行期为类动态添加方法或者字段的过程称为引入。


HUIYL1
7 声望1 粉丝