Spring中AOP的切点、通知、切点表达式及知识要点整理

1.2.1、需要编写的内容

  • 编写核心业务代码(目标类的目标方法)
  • 编写切面类,切面类中有通知(增强功能方法)
  • 在配置文件中,配置织入关系,即将哪些通知与哪些连接点进行结合

1.2.2、AOP 技术实现的内容

Spring 框架监控切入点方法的执行。一旦监控到切入点方法被运行,使用代理机制,动态创建目标对象的代理对象,根据通知类别,在代理对象的对应位置,将通知对应的功能织入,完成完整的代码逻辑运行。

1.2.3、AOP 底层使用哪种代理方式

在 spring 中,框架会根据目标类是否实现了接口来决定采用哪种动态代理的方式。

1.2.4、知识要点

  • aop:面向切面编程
  • aop底层实现:基于JDK的动态代理 和 基于Cglib的动态代理
  • aop的重点概念:
Pointcut(切入点):被增强的方法
Advice(通知/ 增强):封装增强业务逻辑的方法
Aspect(切面):切点+通知
Weaving(织入):将切点与通知结合的过程

开发明确事项:

谁是切点(切点表达式配置)
谁是通知(切面类中的增强方法)
将切点和通知进行织入配置

1.2.5、 基于 XML 的 AOP 开发

1.2.5.1 快速入门

①导入 AOP 相关坐标

②创建目标接口和目标类(内部有切点)

③创建切面类(内部有增强方法)

④将目标类和切面类的对象创建权交给 spring

⑤在 applicationContext.xml 中配置织入关系

⑥测试代码

①导入 AOP 相关坐标

<!--导入spring的context坐标,context依赖aop-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>

<!-- aspectj的织入 -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.13</version>
</dependency>

②创建目标接口和目标类(内部有切点)

//接口
public interface TargetInterface {
 public void method();
}

//实现类
public class Target implements TargetInterface {
 @Override
 public void method() {
 System.out.println("Target running....");
 }
}

③创建切面类(内部有增强方法)

public class MyAspect {
 //前置增强方法
 public void before(){
 System.out.println("前置代码增强.....");
 }
}

④将目标类和切面类的对象创建权交给 spring管理

<!--配置目标类-->
<bean id="target" class="com.itheima.aop.Target"></bean>

<!--配置切面类-->
<bean id="myAspect" class="com.itheima.aop.MyAspect"></bean>

⑤在 applicationContext.xml 中配置织入关系

导入aop命名空间

<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xsi:schemaLocation="
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context.xsd
 http://www.springframework.org/schema/aop
 http://www.springframework.org/schema/aop/spring-aop.xsd
 http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans.xsd">

⑤在 applicationContext.xml 中配置织入关系

配置切点表达式和前置增强的织入关系

<!--配置织入,告诉spring框架哪些方法(切点)要进行增强(前置增强/后置增强)-->
<aop:config>
 <!--声明切面-->
 <!--引用myAspect的Bean为切面对象-->
 <aop:aspect ref="myAspect">
 <!--配置切点-->
 <!--配置Target的method方法执行时要进行myAspect的before方法前置增强-->
 <!--切面:切点+通知, 要配置那个方法需要前置增强(要配置切入点,需要增强的方法) -->
 <aop:before method="before" pointcut="execution(public void com.itheima.aop.Target.method())"></aop:before>
 </aop:aspect>
</aop:config>

⑥测试代码

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class AopTest {
 @Autowired
 private TargetInterface target;
 @Test
 public void test1(){
 target.method();
 }
}

1.2.5.2 切点表达式的写法

表达式语法:

execution([修饰符] 返回值类型 包名.类名.方法名(参数))

  • 访问修饰符可以省略
  • 返回值类型、包名、类名、方法名可以使用星号* 代表任意
  • 包名与类名之间一个点 . 代表当前包下的类,两个点 … 表示当前包及其子包下的类
  • 参数列表可以使用两个点 … 表示任意个数,任意类型的参数列表

例如:

execution(public void com.itheima.aop.Target.method())	//不常用,具体的方法,限制了
execution(void com.itheima.aop.Target.*(..)) //具体的类下的所有方法都会被增强,无返回值

execution(* com.itheima.aop.*.*(..)) //常用(推荐),该包下的任意类的任意方法(参数任意)都会被增强,返回值任意
execution(* com.itheima.aop..*.*(..)) //aop下及其子包下的任意类任意方法都会被增强
execution(* *..*.*(..)) //全部都任意

1.2.5.3 通知的类型

通知的配置语法:

<aop:通知类型 method=“切面类中方法名” pointcut=“切点表达式"></aop:通知类型>

Java类:

//增强对象
public class MyAspect {
 //后置增强
 public void before() {
 System.out.println("前置增强................................");
 }

 //后置增强
 public void afterReturning() {
 System.out.println("后置增强................................");
 }

 //Proceeding JoinPoint 正在执行的链接点----切点
 //环绕增强
 public Object Around(ProceedingJoinPoint point) throws Throwable {
 System.out.println("环绕前增强................................");
 Object proceed = point.proceed();//切点方法
 System.out.println("环绕后增强................................");
 return proceed;
 }

 //异常增强
 public void afterThrowing() {
 System.out.println("异常增强................................");
 }

 //最终增强
 public void after() {
 System.out.println("最终增强................................");
 }
}

xml:

<!--切面:切点+通知, 要配置那个方法需要前置增强(要配置切入点,需要增强的方法) -->
 <!-- <aop:before method="before" pointcut="execution(public void com.lfs.aop.Target.save())"/>-->
 <aop:before method="before" pointcut="execution( * com.lfs.aop.*.*(..))"/>

 <!--后置增强-->
<aop:after-returning method="afterReturning" pointcut="execution(* com.lfs.aop.*.*(..))"/>

 <!--环绕增强-->
 <aop:around method="Around" pointcut="execution(* com.lfs.aop.*.*(..))"/>

 <!--异常增强-->
<aop:after-throwing method="afterThrowing" pointcut="execution(* com.lfs.aop.*.*(..))"/>

 <!--最终增强-->
 <aop:after method="after" pointcut="execution(* com.lfs.aop.*.*(..))"/>

1.2.5.4 切点表达式的抽取

当多个增强的切点表达式相同时,可以将切点表达式进行抽取,在增强中使用 pointcut-ref 属性代替 pointcut 属性来引用抽取后的切点表达式。

//切点表达式
<aop:pointcut id="myPointcut" expression="execution(* com.itheima.aop.*.*(..))"/>
<aop:config>
 <!--引用myAspect的Bean为切面对象-->
 <aop:aspect ref="myAspect">
 <aop:pointcut id="myPointcut" expression="execution(* com.itheima.aop.*.*(..))"/>
 <aop:before method="before" pointcut-ref="myPointcut"></aop:before>
 </aop:aspect>
</aop:config>

1.2.5.5 知识要点

aop织入的配置

<aop:config>
 <aop:aspect ref=“切面类”>
 <aop:before method=“通知方法名称” pointcut=“切点表达式"></aop:before>
 </aop:aspect>
</aop:config>
  • 通知的类型:前置通知、后置通知、环绕通知、异常抛出通知、最终通知
  • 切点表达式的写法:

execution([修饰符] 返回值类型 包名.类名.方法名(参数))

作者:楠黎倾风原文地址:https://blog.csdn.net/qq_64071654/article/details/128029855

%s 个评论

要回复文章请先登录注册