定义普通业务组件
定义切入点,一个切入点可能横切多个业务组件
定义增强处理,增强处理就是在AOP框架为普通业务组件织入的处理动作
定义了增强方法的类叫做切面类 被增强的方法叫切入点
简单的给你讲些基础的知识:
1. AOP:Aspect Oriented Programming(面向切面编程)
2. 利用动态代理实现面向切面编程(底层原理是动态代理这你理解的没错)
3. Spring实现动态代理配置是有两种配置文件:1、 xml文件方式;2、annotation方式(使用AspectJ类库实现的。
4. aspectJ类库,AspectJ是一个专门用来实现动态代理(AOP编程)的类库,AspectJ是面向切面编程的框架,Spring使用就是这个类库实现动态代理的
5. aspectj的专业术语:
1、JoinPoint连接点(切入点)
2、PointCut切入点,当需要定义一个切入点时,则需要使用这个
3、Aspect切面
4、Advice切入点的逻辑
5、Target被代理对象
6、Weave织入
AspectJ与CGLIB的区别
AspectJ:编译时增强,需要自己的编译器来编译java文件,还要织入器
CGLIB:运行时增强
javassist:
AOP方式:
AspectJ方式
1.
aspectjweaver.jar , aspectjrt.jar
2.
<aop:aspectj-autoproxy/>或
<bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator"/>
3.
自动搜索bean组件,自动搜索切面类
<context:component-scan base-package="">
<context:include-filter type="annotation"
expression="org.aspectj.lang.annotation.Aspect"/>
</context:component-scan>
4.
@Aspect
@Before(execution(* lee.*.*(..)))
@After(execution(* lee.*.*(..)))
@AfterReturning(pointcut="execution(* lee.*.*(..))",returning="形参名")--针对Around的返回
@AfterThrowing(pointcut="execution(* lee.*.*(..)",throwing="形参名")
@Around(execution(* lee.*.*(..)))方法第一个参数必须是ProceedingJoinPoint,并执行proceed()方法,并可以传入Ojbect[],
around挺霸道的,不执行proceed()方法就目标方法也不执行
注意:
想访问目标方法参数,可以利用ProceedingJoinPoint,JoinPoint作为参数
顺序:after->around->afterreturning->after
如果execution()&&args(),会找满足参数的切入点
定义个切入点 @Pointcut("execution(* fox.aop.*.*(..))")
public void myPointCut(){}//方法名为切入点名
@Before("myPointCut")//可以使用
@Around("BeforeAdviceTest.myPointCut()")//不在同一个类中
SpringAop方式(AOP与IOC一起来,更精彩)也就是@Component
SpringAop支持的几种切入点指示符
execution:用于匹配执行方法的连接点
execution(public method(..))
within:限定匹配特定类型的连接点,使用SpringAop只能匹配方法执行的连接点
within(package)
this:用于限定AOP代理必须是指定类型的实例,使用SpringAop只能匹配方法执行的连接点
target:用于限定目标对象必须是指定类型的实例,使用SpringAop只能匹配方法执行的连接点
args:对连接点得参数类型限制,使用SpringAop只能匹配方法执行的连接点
bean:指定只匹配指定bean实例内的连接点
bean(*Service)以Service结尾的bean实例内方法执行的连接点
Xml方式
<aop:config>
<aop:pointcut id="i" expression="execution()">
<aop:aspect id="" order="" pointcut-ref="i">--order越小优先级别越高
<aop:before,after...
pointcut,method>
<aop:advisor advice-ref="txAdvice"><!--<tx:advice>事务切面-->