AOP 概念
- AOP:面向切面(方面)编程,扩展功能不修改源代码实现
- AOP采取横向抽取机制,取代了传统纵向继承体系重复性代码
AOP原理
1. 画图分析原理



AOP操作术语
Joinpoint(连接点): 类里面可以被增强的方法,这些方法称为连接点
Pointcut(切入点):所谓切入点是指我们要对哪些Joinpoint进行拦截的定义,实际增强的方法。
*切入点:在类里面可以有很多的方法被增强,比如实际操作中,只是增强了类里面的add方法和updatre方法,实际增强的方法称为切入点。
Advice(通知/增强):所谓通知是指拦截到Joinpoint之后所要做的事情就是通知.通知分为前置通知,后置通知,异常通知,最终通知,环绕通知(切面要完成的功能)
*通知/增强:增强的逻辑,称为增强,比如扩展日志功能,这个日子功能成为增强
**前置通知:在方法之前执行
**后置通知:在方法之后执行
**异常通知:方法出现异常
**最终通知:在后置之后执行
**环绕通知:在方法之前和之后执行
Aspect(切面): 是切入点和通知(引介)的结合
*切面:把增强应用到具体方法上面,过程称为切面,即把增强用到切入点的过程。
Introduction(引介):引介是一种特殊的通知在不修改类代码的前提下, Introduction可以在运行期为类动态地添加一些方法或Field.
Target(目标对象):代理的目标对象(要增强的类)
Weaving(织入):是把增强应用到目标的过程.
把advice 应用到 target的过程
Proxy(代理):一个类被AOP织入增强后,就产生一个结果代理类
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约束
<?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>
使用表达式配置切入点
1.切入点:实际增强的方法
2.常用表达式:
execution(<访问修饰符>?<返回类型><方法名>(<参数>)<异常>)
(1)execution(* cn.itcast.aop.Book,add()) *表示可为任意修饰符
(2)execution(cn.itcast.aop.Book.(…)) 匹配指定包下所有类方法
(3) execution(* .(…)) 匹配对所有类的所有方法
(4)匹配所有save开头的方法execution(* save*(…))
aspectj的AOP操作
<!-- 1.配置对象 -->
<bean id="book" class="cn.itcast.aop.Book"></bean>
<bean id="myBook" class="cn.itcast.aop.MyBook"></bean>
<!-- 2.配置aop操作 -->
<aop:config>
<!-- 2.1 配置切入点 -->
<aop:pointcut expression="execution(* cn.itcast.aop.Book.*(..))" id="ponitcut1"/>
<!-- 2.2 配置切面(把增强放到方法上面) -->
<aop:aspect ref="myBook">
<!-- 配置增强类型
method:增强类里面使用哪个方法作为前置
-->
<aop:before method="before1" pointcut-ref="ponitcut1"/>
</aop:aspect>
</aop:config>
测试代码:
package cn.itcast.aop;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestAnno {
@Test
public void testService() {
ApplicationContext context = new ClassPathXmlApplicationContext("bean3.xml");
Book book = (Book) context.getBean("book");
book.add();
}
}
测试结果:

前面我们测试了前置增强,接下来我们看看环绕增强的使用方法。
首先修改MyBook类,增加around1方法,其中我们增加ProceedingJoinPoint对象来调用切入点的方法,即Book类中的add方法。
public void around1(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
//方法之前
System.out.println("方法之前.......");
//执行被增强的方法
proceedingJoinPoint.proceed();
//方法之后
System.out.println("方法之后.......");
在配置文件的<aop:aspect></aop:aspect>片段中加入
<aop:around method="around1" pointcut-ref="ponitcut1"/>
最后我们来看看测试结果:

本文深入解析面向切面编程(AOP)的概念、原理及术语,如Joinpoint、Pointcut、Advice、Aspect等,并详细阐述如何在Spring框架中通过XML配置和注解方式实现AOP操作,包括配置切入点、切面及增强类型。
174





