spring是开源的轻量级框架——Aop(面向切面)
(一) Aop的概念
1 aop:面向切面(方面)编程,扩展功能不修改源代码实现
2 aop:采取横向抽取机制,取代了传统纵向继承体系重复性代码
3 aop底层使用动态代理实现
(1)第一种情况,有接口情况,使用动态代理创建接口实现类代理对象
(2)第二种情况,没有接口情况,使用动态代理创建类的子类代理对象
(二) Aop原理
AOP操作术语
Joinpoint(连接点): 类里面可以被增强的方法,这些方法称为连接点
Pointcut(切入点):所谓切入点是指我们要对哪些Joinpoint进行拦截的定义.
Advice(通知/增强):所谓通知是指拦截到Joinpoint之后所要做的事情就是通知.通知分为前置通知,后置通知,异常通知,最终通知,环绕通知(切面要完成的功能)
Aspect(切面): 是切入点和通知(引介)的结合
Introduction(引介):引介是一种特殊的通知在不修改类代码的前提下, Introduction可以在运行期为类动态地添加一些方法或Field.
Target(目标对象):代理的目标对象(要增强的类)
Weaving(织入):是把增强应用到目标的过程.
把advice 应用到 target的过程
Proxy(代理):一个类被AOP织入增强后,就产生一个结果代理类
Spring的aop操作
1 在spring里面进行aop操作,使用aspectj实现
(1)aspectj不是spring一部分,和spring一起使用进行aop操作
(2)Spring2.0以后新增了对AspectJ支持
2 使用aspectj实现aop有两种方式
(1)基于aspectj的xml配置
(2)基于aspectj的注解方式
Aop操作准备
1 除了导入基本的jar包之外,还需要导入aop相关的jar包
2 创建spring核心配置文件,导入aop的约束
使用表达式配置切入点
1 切入点:实际增强的方法
2 常用的表达式
execution(<访问修饰符>?<返回类型><方法名>(<参数>)<异常>) (1)execution(* cn.lele.aop.Book.add(..)) (2)execution(* cn.lele.aop.Book.*(..)) (3)execution(* *.*(..)) (4) 匹配所有save开头的方法 execution(* save*(..))
Aspectj的aop操作
<bean id="book" class="com.lele.aop.Book"></bean> <bean id="myBook" class="com.lele.aop.MyBook"></bean> <!-- 2 配置aop操作 --> <aop:config> <!-- 2.1 配置切入点 --> <aop:pointcut expression="execution(* com.lele.aop.Book.*(..))" id="pointcut1"/> <!-- 2.2 配置切面 把增强用到方法上面 --> <aop:aspect ref="myBook"> <!-- 配置增强类型 method: 增强类里面使用哪个方法作为前置 --> <aop:before method="before1" pointcut-ref="pointcut1"/> <aop:after-returning method="after1" pointcut-ref="pointcut1"/> <aop:around method="around1" pointcut-ref="pointcut1"/> </aop:aspect> </aop:config>
java代码实现
public class Book { public void add() { System.out.println("add..........."); } } public class MyBook { public void before1() { System.out.println("前置增强......"); } public void after1() { System.out.println("后置增强......"); } //环绕通知 public void around1(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { //方法之前 System.out.println("方法之前....."); //执行被增强的方法 proceedingJoinPoint.proceed(); //方法之后 System.out.println("方法之后....."); } } <!-- 配置增强类型 method: 增强类里面使用哪个方法作为前置 --> <aop:before method="before1" pointcut-ref="pointcut1"/> <aop:after-returning method="after1" pointcut-ref="pointcut1"/> <aop:around method="around1" pointcut-ref="pointcut1"/> </aop:aspect>
基于aspectj的注解aop
1 使用注解方式实现aop操作
第一步 创建对象 <bean id="book" class="com.lele.aop.Book"></bean> <bean id="myBook" class="com.lele.aop.MyBook"></bean> 第二步 在spring核心配置文件中,开启aop操作 <!--开启aop操作--> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> 第三步 在增强类上面使用注解完成aop操作 @Aspect public class MyBook{ @Before(value="execution(*com.lele.aop.Book.**(...)) public void before1(){ System.out.println("before....."); }
- SpringAOP入门-面向切面编程
- Spring 面向切面编程AOP
- spring面向切面编程AOP(Aspect-orented programming)
- 【spring-boot】spring aop 面向切面编程初接触
- Spring 面向切面,简单入门(AOP)
- Spring--AOP--面向切面编程
- Spring核心机制(面向切面编程AOP)
- 【spring-boot】spring aop 面向切面编程初接触--切点表达式
- Spring 面向切面编程AOP
- Spring学习一:IOC(控制反转)和AOP(面向切面)的xml配置和注解方式
- Spring学习4-面向切面(AOP)之schema配置方式
- Spring面向切面编程——Spring实现AOP方式——自定义类实现
- Spring AOP面向切面编程之日志记录
- Spring面向切面编程之AOP代理模式
- Spring AOP:面向切面编程简介
- spring学习--面向切面编程AOP和AspectJ
- Spring核心机制(面向切面编程AOP)详解
- Spring学习4-面向切面(AOP)之Spring接口方式
- Spring AOP 面向切面编程相关注解
- spring AOP面向切面编程的四种实现方式