SpringAOP 底层实现方式
1:基于jdk的动态代理(需要源对象必须实现至少一个接口)
通过Proxy.newProxyInstance(Classloader,Interfaces.class,InvocationHandler)
new InvocationHandler(){
public Object invoke(Object proxy, Method method(源对象的方法对象), Object[] args(该方法传递的参数))
}
---------------------
丨JDK的动态代理(接口)丨
---------------------
TaetgetInterface proxyTarget =(TargetInterFace) Proxy.newInstance(ClassLoader,Class[] Interfaces.class, new InvocationHandler(){
@Override
public Object invoke(Object proxy, Mehtod method, Object...args){
//前置增强代码 可以选择增强参数等,也可以做不关对象方法本身的操作
Object invoke = method.invoke(target,args);
//后置增强代码,可以选择增强返回值等,也可以做其他操作
}
});
proxyTarget.show();(以后使用的target对象调用的show方法 都是使用代理对象proxyTarget调用);
--------------------------------------
丨 CGlib实现动态代理(无接口,非final) 丨
--------------------------------------
Enhancer enhancer = new Enhancer();
enhancer.setSuperClass(Target.class);
enhancer.setCallBack(new MethodInterceptor(){
@Override
public Object interceptor(Object proxy, Mehtod method, Object...args, MethodProxy methodProxy){
//前置增强方法
Object invoke = method.invoke(target,args);
//后置增强方法
}
});
Target proxyTarget = enhancer.create(); //CGlib的原理是使用继承的思想,所以内存中动态生成的代理对象是Target的子类对象
proxyTarget.show(); (以后使用的target对象调用的show方法 都是使用代理对象proxyTarget调用);
生成与源对象一样实现相同接口的类.当源对象调用某方法的时候,事实是生成的代理对象调用invoke方法.
可以在invoke方法中使用反射调用源对象的该method方法 method.invoke(源对象,args);
args可以做更改;该method方法执行后的返回值,也可以更改,称之为 参数增强和返回值增强
2:基于CGlib的动态代理(将源对象作为父类,运行期在内存中生成该源对象类的子类) 引入依赖aspectjweaver 缺点:无法实现final修饰的方法.私有属性修饰的方法不确定.
通过Enhancer enhancer = new Enhancer();
enhancer.setSuperClass(源对象类字节码对象);
enhancer.setCallBack(方法拦截器对象(接口) new MethodInterceptor(){
public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy){}
})此处的intercept方法类似于jdk动态设计模式的invoke方法.多的MethodProxy对象是该方法的代理执行对象,可以实现部分method对象的功能
SpringAOP 在Spring的配置文件中如何配置?
1:XML文档配置
引入依赖aspectjweaver 引入aop约束;
定义切面类的bean
<bean id="myAspect" class="com.itheima.MyAspect">
<aop:config>
<aop:aspect id="aspect" ref="myAspect">
id代表给该pointcut设置的唯一标识,第一个 * 代表 返回值 (权限修饰可以省略) 方法参数里面的..代表任意参数类型和数量
包后面的..代表该包下的直接或者间接的所有类
<aop:pointcut id="point1" expression="execution(* *.*..*(..))">
按执行先后顺序
<aop:around method="切面类中的around方法" pointcut="point1">
<aop:around method="切面类中的around方法" pointcut="execution(void com.itheima.Target.getStr(int, int))">
<aop:around method="切面类中的around方法" pointcut="execution(void com.itheima.Target.getStr(..))">
<aop:around method="切面类中的around方法" pointcut="execution(void com.itheima.Target.*(..))">
<aop:around method="切面类中的around方法" pointcut="execution(void com.itheima.*.*(..))">
## *** 最常用 *** ## (itheima包下的所有直接类的所有方法.不管参数和返回值类型)
<aop:around method="切面类中的around方法" pointcut="execution(* com.itheima.*.*(..))">
iehtima包下的所有直接类或者间接类的所有方法,不论参数和返回值类型
<aop:around method="切面类中的around方法" pointcut="execution(* com.itheima..*.*(..))">
<aop:before method="切面类中的before方法" pointcut="point1">
<aop:around method="切面类中的around方法" pointcut="point1">
<aop:after method="切面类中的after方法" pointcut="point1"> 在spring'中默认被finally修饰
<aop:afterReturning method="切面类中的afterReturning方法" pointcut="point1">
此处可以指定returning = "returnValue" 指定参数Object returnValue为源对象执行源方法后的返回值为returnValue.此时可以做增强返回值操作
<aop:afterThrowing method="切面类中的afterThrowing方法" point="point1"> 尽在方法抛出未被捕获的异常的时候执行
</aop:aspect>
</aop:config>
SpringAOP 注解:
使用注解,一定要定义组件扫描:否则Spring不会去扫描定义类上的注解
<context:component-scan base-package="com.itheima"/>
而且需要设置aop自动代理
<aop:aspectj-autoproxy/>
注解:
@EnableAspectJAutoProxy(aop的aspectj自动代理 == <aop:aspectj-autoproxy/>)
@Aspect("定义该类为一个切面类"),依赖于该类的组件注解@Component("myAspect")
@Around(value = "execution(* com.itheima.*.*(..))") 定义环绕增强方法
@Before(value = "execution(* com.itheima.*.*(..))") 定义前置增强方法.
@Around(value = "execution(* com.itheima.*.*(..))") 定义环绕增强方法
@After(value = "execution(* com.itheima.*.*(..))") 定义后置增强方法(finally修饰)
@AfterReturning(value = "execution(* com.itheima.*.*(..))") 定义后置增强方法
此处可以指定returning = "returnValue" 指定参数Object returnValue为源对象执行源方法后的返回值为returnValue.此时可以做增强返回值操作
@AfterReturning(returning="returnValue" value="execution(* com.itheima.*.*(..))")
@AfterThrowing(value = "execution(* com.itheima.*.*(..)") 定义出现未被捕获的异常的时候执行的方法
@Pointcut(value = "execution(* com.itheima.*.*(..))") 定义在任意方法上,该方法的全路径方法名可以作为该value的替代命名;
private void do(){}
以上的Around,Before,After等注解可以简写为:
@Around("do()") value = 是默认属性,本身就可以省略.