1、概述
1、动态代理
1、有接口的情况,使用的是jdk动态代理
2、没有接口,使用的是cglib动态代理
2、相关术语
1、连接点:可以被代理的方法
2、切入点:被代理的方法
3、增强方法:例如日志方法就叫增强方法
4、切面:增加日志方法写在被代理的方法中,就叫切面
3、表达式
execution("* 包名.*.*(..)")
1、第一个*:方法任意的返回值
2、第二个*:包下所有的类
3、第三个*:包下所有的方法
4、(..):方法任意的参数
4、通知类型
1、before:前置通知
2、after-returning:后置通知
3、around:环绕通知,形參加入ProceedingJoinPoint joinPoint,执行joinPoint.proceed()等于执行目标方法
4、after-throwing:异常通知
5、after:最终通知,肯定会执行的通知,类似于finally
5、xml方式
id:随便起
expression:表达式
aop:aspect ref:增加类
method:增强方法
<aop:config>
<aop:pointcut id="userDao" expression="execution(* aopDemo.UserDaoImpl.*(..))"/>
<aop:aspect ref="logger">
<aop:before method="writerLog" pointcut-ref="userDao"></aop:before>
</aop:aspect>
</aop:config>
6、注解方式
1、添加专门扫描aop注解的xml
<aop:aspectj-autoproxy ></aop:aspectj-autoproxy>
2、切面类上增加@Aspect,表名是一个切面类
3、方法上增加通知类型注解,以环绕通知举例:@Around("execution(* aopDemo.UserDaoImpl.*(..))")
7 、JointPoint
获取到被代理的对象,也就能获取到所有的信息
8、ProceedingJoinPoint
环绕通知专属,继承与JoinPoint,特别的方法是proceed,用来表示执行的方法