欢迎光临散文网 会员登陆 & 注册

Spring-aop

2020-07-31 16:54 作者:某个峙祁  | 我要投稿

Spring 中的 AOP
1: AOP 相关术语
Joinpoint(连接点):
所谓连接点是指那些被拦截到的点。在 spring 中,这些点指的是方法,因为 spring 只支持方法类型的 连接点。

Pointcut(切入点):
所谓切入点是指我们要对哪些 Joinpoint 进行拦截的定义。

Advice(通知/增强):
所谓通知是指拦截到 Joinpoint 之后所要做的事情就是通知。
通知的类型:前置通知,后置通知,异常通知,最终通知,环绕通知。

Introduction(引介):
引介是一种特殊的通知在不修改类代码的前提下, Introduction 可以在运行期为类动态地添加一些方 法或 Field。

Target(目标对象):
代理的目标对象。

Weaving(织入):
是指把增强应用到目标对象来创建新的代理对象的过程。 spring 采用动态代理织入,而 AspectJ 采用编译期织入和类装载期织入。

Proxy(代理):
一个类被 AOP 织入增强后,就产生一个结果代理类。

Aspect(切面): 是切入点和通知(引介)的结合。

##环境搭建

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

##配置步骤
###第一步:创建 spring 的配置文件并导入约束
```xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:aop="http://www.springframework.org/schema/aop"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">

</beans>
```
### 第二步:把通知类用 bean 标签配置起来
   <!--日志-->
   <bean id="logger" class="com.tc51.util.Logger"></bean>
###第三步::使用 aop:config 声明 aop 配置
aop:config:
作用:用于声明开始 aop 的配置
   <aop:config>
     <!-- 配置的代码都写在此处 -->
  </aop:config>
###第四步:使用 aop:aspect 配置切面
aop:aspect:
作用:
  用于配置切面。
属性:
  id:给切面提供一个唯一标识。
  ref:引用配置好的通知类 bean 的 id。
<aop:aspect id="txAdvice" ref="txManager">
  <!--配置通知的类型要写在此处-->
</aop:aspect>

###第五步:使用 aop:pointcut 配置切入点表达式
aop:pointcut:
作用:
  用于配置切入点表达式。就是指定对哪些类的哪些方法进行增强。
属性:
  expression:用于定义切入点表达式。
  id:用于给切入点表达式提供一个唯一标识
<aop:pointcut expression="execution(
  public void com.tc51.service.impl.AccountServiceImpl.transfer(
java.lang.String, java.lang.String, java.lang.Float)
)" id="pt1"/>

###第六步:使用 aop:xxx 配置对应的通知类型
aop:before
作用:
  用于配置前置通知。指定增强的方法在切入点方法之前执行
属性:
  method:用于指定通知类中的增强方法名称
  ponitcut-ref:用于指定切入点的表达式的引用
  poinitcut:用于指定切入点表达式
执行时间点:
  切入点方法执行之前执行
<aop:before method="beginTransaction" pointcut-ref="pt1"/>
 
aop:after-returning
作用:
  用于配置后置通知
属性:
  method:指定通知中方法的名称。
  pointct:定义切入点表达式
  pointcut-ref:指定切入点表达式的引用
执行时间点:
  切入点方法正常执行之后。它和异常通知只能有一个执行
     <aop:after-returning method="commit" pointcut-ref="pt1"/>
       
aop:after-throwing
作用:
  用于配置异常通知
属性:
  method:指定通知中方法的名称。
  pointct:定义切入点表达式
  pointcut-ref:指定切入点表达式的引用
  执行时间点:
切入点方法执行产生异常后执行。它和后置通知只能执行一个
<aop:after-throwing method="rollback" pointcut-ref="pt1"/>
 
aop:after
作用:
  用于配置最终通知
属性:
  method:指定通知中方法的名称。
  pointct:定义切入点表达式
  pointcut-ref:指定切入点表达式的引用
执行时间点:
  无论切入点方法执行时是否有异常,它都会在其后面执行。
<aop:after method="release" pointcut-ref="pt1"/>

##环绕通知
```xml

<aop:config>
  <aop:pointcut expression="execution(* com.itheima.service.impl.*.*(..))"
id="pt1"/>
     <aop:aspect id="txAdvice" ref="txManager">
     <!-- 配置环绕通知 -->
     <aop:around method="transactionAround" pointcut-ref="pt1"/>
  </aop:aspect>
</aop:config>

```

```java

public class Logger {

   public void log() {
       System.out.println("记录日志...");
   }

   /**
    * 环绕通知
    *
    * @param pjp spring 框架为我们提供了一个接口:ProceedingJoinPoint,它可以作为环绕通知的方法参数。
    *            在环绕通知执行时,spring 框架会为我们提供该接口的实现类对象,我们直接使用就行。
    * @return
    */


   public Object log2(ProceedingJoinPoint joinPoint) {
       Object proceed = null;
       try {
           //获取方法执行所需的参数
           Object[] args = joinPoint.getArgs();
           proceed = joinPoint.proceed(args);
       } catch (Throwable throwable) {
           throwable.printStackTrace();
       } finally {
       }
       System.out.println("环绕记录日志...");
       return proceed;
   }
}

```








Spring-aop的评论 (共 条)

分享到微博请遵守国家法律