0
点赞
收藏
分享

微信扫一扫

Spring学习(8) AOP


  • ​​AspectJ框架[AOP框架]​​
  • ​​使用AspectJ步骤​​
  • ​​Spring中AOP概述​​
  • ​​Spring中AOP相关术语​​

AspectJ框架[AOP框架]

  • AspectJ是Java社区里最完善最流行的AOP框架
  • 在Spring2.0以上版本中,可以使用基于AspectJ注解或基于XML配置的AOP

使用AspectJ步骤

  1. 添加jar包支持

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.3.18</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.7</version>
<scope>compile</scope>
</dependency>

  1. 配置文件
  • 开启组件扫描
  • 开启AspectJ注解支持

<?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:context="http://www.springframework.org/schema/context"
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/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">

<!-- 开启组件扫描-->
<context:component-scan base-package="com.zyh.aop"></context:component-scan>

<!-- 开启AspectJ注解支持-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

</beans>

  1. 把MyLogging类上面添加注解
    @Component:把当前类标识为组件
    @Aspect:把当前类标识为切面类[非核心业务提取类]
  2. 把MyLogging中的方法添加通知注解
    @Before
  3. Spring学习(8) AOP_java

  4. 测试
  5. Spring学习(8) AOP_xml_02

Spring中AOP概述

  • AOP: Aspect-Oriented Programming 面向切面编程
  • 优势 :
  • 解决代码分散问题
  • 解决代码混乱问题
  • OOP: Object-Oriented Programming :面向对象编程

Spring中AOP相关术语

  • ①横切关注点 :非核心业务代码,称之为横切关注点
  • ②切面(Aspect):把横切关注点提取到一个类,这个类称为切面类
  • ③通知(Advice):把横切关注点提取到类中以后,横切关注点更名为通知
  • ④目标(Target):目标对象,指的是需要被代理的对象[实现类CalcImpl]
  • ⑤代理(Proxy):代理对象可以理解为中介
  • ⑥连接点(JoinPointer):通知方法需要指定通知位置,这个位置称为连接点[通知之前]
  • ⑦切入点(pointcut):通知方法需要指定通知位置,这个位置称为切入点[通知之后]


举报

相关推荐

0 条评论