AOP的术语
1、 目标类target:就是我们需要增强的那个类
2、 代理类proxy:就是自定义的代理的对象 $Proxy0.class
3、 连接点joinPoint:程序执行的某个特定位置,Spring仅支持方法的连接点
4、 切入点pointCut:就是在目标类中实际增强的方法
5、 织入weave:就是将代理类中增强的方法放入到目标类中去执行的过程
将原方法与其他类的方法一起调用
6、 引介Introduction:引介是一种特殊的增强,它为类添加一些属性和方法(课程不使用)
7、 通知advice:将代理对象中的方法应用到目标类的过程中产生的结果。
8、 切面aspect:所有的切入点和代理对象的方法组成在一起 构成了切面
aop依赖包
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.7.2</version>
</dependency>
AOP切面编程-切面表达式
(1)什么是切面表达式
execution([修饰符] 返回值类型 包.类.方法(参数列表) );
(2)切面表达式有什么用?
符合表达式的方法,会被增强
- 使用* 表过任意的内容
- 使用
..
可以表示包与子包下面的类 - 使用
..
可以写在方法(…)表示任意参数
AOP切面编程-增强方式
(1)
增强方式的方式有哪些?
实质是增强的四个可选调用位置
在这里插入图片描述
(2)每个位置有四个对应的名称
<!-- 配置他们的增强关系 生成一个代理类-->
<aop:config >
<aop:pointcut id="all" expression="execution( * com.wzx.service..*.*(..))"/>
<!-- 我想在work方法执行 之后调用一下writeLog-->
<aop:aspect ref="advice">
<!-- try-->
<aop:before method="before" pointcut-ref="all"/>
<!-- 有可能程序正常运行-->
<aop:after-returning method="afterReturn" pointcut-ref="all"/>
<!-- catch-->
<aop:after-throwing method="afterThrow" pointcut-ref="all"/>
<!-- finally-->
<aop:after method="after" pointcut-ref="all"/>
</aop:aspect>
</aop:config>
(3)上面我们需要一个一个指定增强位置,我们可以使用arround来指定方法通过方法来实现增强位置
(类似写代理类的增强逻辑)
arround环绕
<!-- 配置他们的增强关系 生成一个代理类-->
<aop:config >
<aop:pointcut id="all" expression="execution( * com.wzx.service..*.*(..))"/>
<!-- 我想在work方法执行 之后调用一下writeLog-->
<aop:aspect ref="advice">
<aop:around method="arround" pointcut-ref="all"/>
</aop:aspect>
</aop:config>
public void arround(ProceedingJoinPoint point ){//参目标类中的任意方法
try{
//执行before
before();
System.out.println("arround-------");
point.proceed();//对应你调用的UserImpl中的任意方法
//执行正常返回
afterReturn();
}catch (Throwable e){
//补救方法
afterThrow();
}finally {
//释放资源的方法
after();
}
}