术语:
1.通知(Advice):
通知定义了切面是什么以及何时使用。描述了切面要完成的工作和何时需要执行这个工作。
2.连接点(Joinpoint):
程序能够应用通知的一个“时机”,这些“时机”就是连接点,例如方法被调用时、异常被抛出时等等。
3.切入点(Pointcut)
通知定义了切面要发生的“故事”和时间,那么切入点就定义了“故事”发生的地点,例如某个类或方法的名称,Spring中允许我们方便的用正则表达式来指定
4.切面(Aspect)
通知和切入点共同组成了切面:时间、地点和要发生的“故事”
5.引入(Introduction)
引入允许我们向现有的类添加新的方法和属性(Spring提供了一个方法注入的功能)
6.目标(Target)
即被通知的对象,如果没有AOP,那么它的逻辑将要交叉别的事务逻辑,有了AOP之后它可以只关注自己要做的事(AOP让他做爱做的事)
7.代理(proxy)
应用通知的对象,详细内容参见设计模式里面的代理模式
8.织入(Weaving)
把切面应用到目标对象来创建新的代理对象的过程,织入一般发生在如下几个时机:
(1)编译时:当一个类文件被编译时进行织入,这需要特殊的编译器才可以做的到,例如AspectJ的织入编译器
(2)类加载时:使用特殊的ClassLoader在目标类被加载到程序之前增强类的字节代码
(3)运行时:切面在运行的某个时刻被织入,SpringAOP就是以这种方式织入切面的,原理应该是使用了JDK的动态代理技术
配置:
常用的表达式
execution(<访问修饰符>?<返回类型><方法名>(<参数>)<异常>)
(0)execution(public void cn.UserDao.update(..))
* 如果访问修饰符不写* ,比如写public,返回类型必须要写出来
(1)execution(*cn.UserDao.update(..))
* 增强UserDao类里面update方法
(2)execution(*cn.UserDao.*(..))
* 增强UserDao类里面所有方法
(3)execution(* *.*(..))
* 所有增强
(4)execution(* save*(..))
- 对方法名称,如果save开头的进行增强
(5)execution(* cn.dao..*(..))
- 两个点表示当前包和子包,孙包,类方法进行增强
(6)execution(* cn.dao.GenericDAO+.*(..))
---- + ;针对接口情况,接口里面所有实现类
3 通知类型
前置通知:在方法之前执行
后置通知:在方法之后执行
环绕通知:在方法之前和之后执行
异常通知:被增强的方法运行过程中,出现异常,执行这个通知
最终通知:类似于finally,总会被执行
<?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">
<bean id="user" class="cn.aop.User"></bean>
<bean id="myUser" class="cn.aop.MyUser"></bean>
<!-- aop配置 -->
<aop:config>
<!-- 配置切入点
expression:使用表达式配置切入点
execution(* cn.aop.User.*(..))
-->
<aop:pointcut expression="execution(* cn.aop.User.*(..))" id="p1"/>
<!-- 配置切面 -->
<aop:aspect ref="myUser">
<!-- 环绕通知 -->
<aop:around method="around1" pointcut-ref="p1"/>
<!-- 配置前置通知 作为前置通知的增强的方法名称-->
<aop:before method="before1" pointcut-ref="p1"/>
<!-- 配置后置通知 -->
<aop:after-returning method="after1" pointcut-ref="p1"/>
<!-- 异常通知 -->
<aop:after-throwing method="throw1" pointcut-ref="p1"/>
<!-- 最终通知 -->
<aop:after method="finally1" pointcut-ref="p1"/>
</aop:aspect>
</aop:config>
</beans>