Spring框架技术总结(二)

  • 归属:beans标签

  • 作用:设置AOP:设置当前类为切面类

  • 格式:

aop:config……</aop:config>

aop:config……</aop:config>

  • 说明:一个beans标签中可以配置多个aop:config标签

2.1.2、aop:aspect

  • 名称:aop:aspect

  • 类型:标签

  • 归属:aop:config标签

  • 作用:设置具体的AOP通知对应的切入点

  • 格式:

aop:config

<aop:aspect ref=“beanId”>……</aop:aspect>

<aop:aspect ref=“beanId”>……</aop:aspect>

</aop:config>

  • 说明:

一个aop:config标签中可以配置多个aop:aspect标签

  • 基本属性:

  • ref :通知所在的bean的id

2.1.3、aop:pointcut

  • 名称:aop:pointcut

  • 类型:标签

  • 归属:aop:config标签、aop:aspect标签

  • 作用:设置切入点

  • 格式:

aop:config

<aop:pointcut id=“pointcutId” expression=“……”/>

aop:aspect

<aop:pointcut id=“pointcutId” expression=“……”/>

</aop:aspect>

</aop:config>

  • 说明:

一个aop:config标签中可以配置多个aop:pointcut标签,且该标签可以配置在aop:aspect标签内

  • 基本属性:

  • id :识别切入点的名称

  • expression :切入点表达式

2.1、切点表达式


切入点

  • 切入点描述的是某个方法

  • 切入点表达式是一个快速匹配方法描述的通配格式,类似于正则表达式

表达式语法

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

execution(public void com.itheima.aop.Target.save())

  • 访问修饰符可以省略

  • 返回值类型、包名、类名、方法名可以使用星号 * 代表任意

  • 包名和类名之间一个点 . 代表当前包下的类,两个点 … 表示当前包及其子包下的类

  • 参数列表可以使用两个点 … 表示任意个数,任意类型的参数列表

  • *:单个独立的任意符号,可以独立出现,也可以作为前缀或者后缀的匹配符出现

execution(public * com.itheima..UserService.find(*))

  • 匹配com.itheima包下的任意包中的UserService类或接口中所有find开头的带有一个参数的方法

  • .. :多个连续的任意符号,可以独立出现,常用于简化包名与参数的书写

execution(public User com…UserService.findById(…))

  • 匹配com包下的任意包中的UserService类或接口中所有名称为findById的方法

例如:

execution(public void com.itheima.aop.Target.method())

execution(void com.itheima.aop.Target.*(…))

execution(* com.itheima.aop..(…))

execution(* com.itheima.aop….(…))

execution(* .*(…))

2.2、通知的类型


通知的配置语法:

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

| 名称 | 标签 | 说明 |

| — | — | — |

| 前置通知 | <aop:before> | 用于配置前置通知。指定增强的方法在切入点方法之前执行 |

| 后置通知 | <aop:after-returning> | 用于配置后置通知。指定增强的方法在切入点方法之后执行 |

| 环绕通知 | <aop:around> | 用于配置环绕通知。指定增强的方法在切入点方法之前和之后都执行 |

| 异常抛出通知 | <aop:throwing> | 用于配置异常抛出通知。指定增强的方法在出现异常时执行 |

| 最终通知 | <aop:after> | 用于配置最终通知。无论增强方式执行是否有异常都会执行 |

2.2.1、aop:before

  • 名称:aop:before

  • 类型:标签

  • 归属:aop:aspect标签

  • 作用:设置前置通知

  • 格式:

<aop:aspect ref=“adviceId”>

<aop:before method=“methodName” pointcut=“……”/>

</aop:aspect>

  • 说明:一个aop:aspect标签中可以配置多个aop:before标签

  • 基本属性:

  • method :在通知类中设置当前通知类别对应的方法

  • pointcut:设置当前通知对应的切入点表达式,与pointcut-ref属性冲突

  • pointcut-ref:设置当前通知对应的切入点id,与pointcut属性冲突

2.2.2、aop:after-returning

  • 名称:aop:after-returning

  • 类型:标签

  • 归属:aop:aspect标签

  • 作用:设置返回后通知

  • 格式:

<aop:aspect ref=“adviceId”>

<aop:after-returning method=“methodName” pointcut=“……”/>

</aop:aspect>

  • 说明:一个aop:aspect标签中可以配置多个aop:after-returning标签

  • 基本属性:

  • method :在通知类中设置当前通知类别对应的方法

  • pointcut:设置当前通知对应的切入点表达式,与pointcut-ref属性冲突

  • pointcut-ref:设置当前通知对应的切入点id,与pointcut属性冲突

2.2.2、aop:around

  • 名称:aop:around

  • 类型:标签

  • 归属:aop:aspect标签

  • 作用:设置环绕通知

  • 格式:

<aop:aspect ref=“adviceId”>

<aop:around method=“methodName” pointcut=“……”/>

</aop:aspect>

  • 说明:一个aop:aspect标签中可以配置多个aop:around标签

  • 基本属性:

  • method :在通知类中设置当前通知类别对应的方法

  • pointcut :设置当前通知对应的切入点表达式,与pointcut-ref属性冲突

  • pointcut-ref :设置当前通知对应的切入点id,与pointcut属性冲突

2.2.3、aop:after-throwing

  • 名称:aop:after-throwing

  • 类型:标签

  • 归属:aop:aspect标签

  • 作用:设置抛出异常后通知

  • 格式:

<aop:aspect ref=“adviceId”>

<aop:after-throwing method=“methodName” pointcut=“……”/>

</aop:aspect>

  • 说明:一个aop:aspect标签中可以配置多个aop:after-throwing标签

  • 基本属性:

  • method :在通知类中设置当前通知类别对应的方法

  • pointcut :设置当前通知对应的切入点表达式,与pointcut-ref属性冲突

  • pointcut-ref :设置当前通知对应的切入点id,与pointcut属性冲突

2.2.4、示例

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

public class MyAspect {

//前置增强

public void before(){

System.out.println(“前置增强…”);

}

//后置增强

public void afterReturning(){

System.out.println(“后置增强…”);

}

//环绕增强

//Proceeding JoinPoint:正在执行的连接点 == 切点

public Object around(ProceedingJoinPoint pjp){

System.out.println(“环绕前增强…”);

Object proceed = pjp.proceed();

System.out.println(“环绕后增强…”);

return proceed;

}

//异常抛出增强

public void afterThrowing(){

System.out.println(“异常抛出增强…”);

}

//最终通知增强

public void after(){

System.out.println(“最终通知增强…”);

}

}

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

aop:config

<aop:aspect ref=“myAspect”>

<aop:before method=“myPointcut” pointcut=“execution(* com.itheima.aop..(…))”/>

<aop:around method=“around” pointcut=“execution(* com.itheima.aop..(…))” />

<aop:after-returning method=“afterRetruning” pointcut=“(* com itheima.aop..(…))” />

<aop:after-returning method=“afterThrowing” pointcut=“(* com itheima.aop..(…))” />

<aop:after method=“after” pointcut=“(* com itheima.aop..(…))” />

</aop:aspect>

</aop:config>

2.3、切点表达式的抽取


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

aop:config

<aop:aspect ref=“myAspect”>

<aop:pointcut id=“myPointcut” expression=“execution(* com.itheima.aop..(…))”></aop:pointcut>

<aop:before method=“myPointcut” pointcut-ref=“myPointcut”/>

<aop:around method=“around” pointcut-ref=“myPointcut” />

<aop:after-returning method=“afterRetruning” pointcut-ref=“myPointcut” />

<aop:after-returning method=“afterThrowing” pointcut-ref=“myPointcut” />

<aop:after method=“after” pointcut-ref=“myPointcut” />

</aop:aspect>

</aop:config>

2.4、知识要点


  • aop 织入的配置

aop:config

<aop:aspect ref=“切面类”>

<aop:before method=“通知方法名称” pointcut=“切点表达式”></aop:before>

</aop:aspect>

</aop:config>

  • 通知的类型:前置通知、后置通知、环绕通知、异常抛出通知、最终通知

  • 切点表达式的写法:

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

2.5、基于注解的AOP开发


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

public interface TargetInterface {

public void method();

}

public class Target implements TargetInterface {

@Override

public void method(){

System.out.println(“Target running…”);

}

}

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

public class MyAspect {

//前置增强方法

public void before() {

System.out.println(“前置代码增强…”);

}

}

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

@Component(“target”)

public class Target implements TargetInterface {

@Override

public void method() {

System.out.println(“Target running…”);

}

}

@Component(“myAspect”)

public class MyAspect {

public void before() {

System.out.println(“前置代码增强…”);

}

}

  1. 在切面类中使用注解配置织入关系

@Component(“myAspect”)

@Aspect

public class MyAspect {

@Before(“execution(* com.itheima.aop..(…))”)

public void before(){

System.out.println(“前置代码增强…”);

}

}

  1. 在配置文件中开启组件扫描和 AOP 的自动代理

<context:component-scan base-package=“com.itheima.aop”/>

aop:aspectj-autoproxy</aop:aspectj-autoproxy>

  1. 测试代码

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(“classpath:applicationContext.xml”)

public class AopTest {

@Autowired

private TargetInterface target;

@Test

public void test1() {

target.method();

}

}

3、注解配置AOP详解

=============================================================================

3.1、注解开发AOP制作步骤


在XML格式基础上

  • 导入坐标(伴随spring-context坐标导入已经依赖导入完成)

  • 开启AOP注解支持

  • 配置切面@Aspect

  • 定义专用的切入点方法,并配置切入点@Pointcut

  • 为通知方法配置通知类型及对应切入点@Before

3.2、注解通知的类型


通知的配置语法:@通知注解("切点表达式")

| 名称 | 注解 | 说明 |

| — | — | — |

| 前置通知 | @Before | 用于配置前置通知。指定增强的方法在切入点方法之前执行 |

| 后置通知 | @AfterReturning | 用于配置后置通知。指定增强的方法在切入点方法之后执行 |

| 环绕通知 | @Around | 用于配置环绕通知。指定增强的方法在切入点方法之前和之后都执行 |

| 异常抛出通知 | @AfterThrowing | 用于配置异常抛出通知。指定增强的方法在出现异常时执行 |

| 最终通知 | @After | 用于配置最终通知。无论增强方式执行是否有异常都会执行 |

3.1.1、@Aspect

最近我根据上述的技术体系图搜集了几十套腾讯、头条、阿里、美团等公司21年的面试题,把技术点整理成了视频(实际上比预期多花了不少精力),包含知识脉络 + 诸多细节,由于篇幅有限,这里以图片的形式给大家展示一部分

===========================================

3.1、注解开发AOP制作步骤


在XML格式基础上

  • 导入坐标(伴随spring-context坐标导入已经依赖导入完成)

  • 开启AOP注解支持

  • 配置切面@Aspect

  • 定义专用的切入点方法,并配置切入点@Pointcut

  • 为通知方法配置通知类型及对应切入点@Before

3.2、注解通知的类型


通知的配置语法:@通知注解("切点表达式")

| 名称 | 注解 | 说明 |

| — | — | — |

| 前置通知 | @Before | 用于配置前置通知。指定增强的方法在切入点方法之前执行 |

| 后置通知 | @AfterReturning | 用于配置后置通知。指定增强的方法在切入点方法之后执行 |

| 环绕通知 | @Around | 用于配置环绕通知。指定增强的方法在切入点方法之前和之后都执行 |

| 异常抛出通知 | @AfterThrowing | 用于配置异常抛出通知。指定增强的方法在出现异常时执行 |

| 最终通知 | @After | 用于配置最终通知。无论增强方式执行是否有异常都会执行 |

3.1.1、@Aspect

[外链图片转存中…(img-69y9T9lD-1714151084867)]

最近我根据上述的技术体系图搜集了几十套腾讯、头条、阿里、美团等公司21年的面试题,把技术点整理成了视频(实际上比预期多花了不少精力),包含知识脉络 + 诸多细节,由于篇幅有限,这里以图片的形式给大家展示一部分

[外链图片转存中…(img-DmnVSKj6-1714151084868)]

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值