当前位置:网站首页>动态代理
动态代理
2022-07-15 14:36:00 【꧁এ悲宸๓₯㎕】
概念:
动态代理是在运行时,通过反射机制动态生成代理类。开发者不需要手动编写新的代理类。
动态代理的分类
1,JDK动态代理
2,CGLib动态代理
JDK动态代理
JDK自带的,前提是:被代理类必须实现过接口。
实现步骤
1) 实现InvocationHandler接口
2)实现invoke方法
3)通过Proxy.newProxyInstance方法返回代理对象
public class JdkShop implements InvocationHandler {
private Object target;
public Object newProxyInstance(Object target){
this.target = target;
//1,类加载器 2,接口 3,InvocationHandler的实现对象
return Proxy.newProxyInstance(target.getClass().getClassLoader(),
target.getClass().getInterfaces(),
this);
}
/** * 方法调用 * @param proxy * @param method * @param args * @return * @throws Throwable */
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("为商品打广告");
//调用原来的方法
Object invoke = method.invoke(target, args);
System.out.println("为商品做售后");
return invoke;
}
}
CGLib动态代理
需要引入CGLib依赖,它的原理是:通过反射+继承机制动态生成被代理类的子类,所以被代理类不能是final的。
1)引入cglib
2)实现MethodInterceptor接口
3)实现intercept方法
4)通过Ehancer返回代理对象
/** * CGLib动态代理 */
public class CGlibFactoryProxy implements MethodInterceptor {
//被代理对象
private Object target;
public Object newProxyInstance(Object target){
this.target = target;
//创建CGLib的增强器对象
Enhancer enhancer = new Enhancer();
//给增强器设置父类
enhancer.setSuperclass(target.getClass());
//设置方法拦截器的实现对象
enhancer.setCallback(this);
//返回代理对象
return enhancer.create();
}
/** * 拦截方法 * @param o * @param method * @param objects * @param methodProxy * @return * @throws Throwable */
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
System.out.println("CGLib给你打广告");
Object obj = method.invoke(target, objects);
System.out.println("CGLib给你售后服务");
return obj;
}
}
AOP的实现原理
容器调用getBean
1,如果被AOP切中
1,实现接口----生成JDK动态代理对象 2,没有实现接口----生成CGLib动态代理对象2,没有被AOP切中,返回原始类对象
当调用容器的getBean方法后,Spring查找对象后会判断该对象的方法是否被某个切面切中
如果没有切中,就创建原有类的对象
如果被切中了,再判断该类是否实现过任何接口
如果实现过接口,则通过JDK动态代理生成代理类,并创建对象
如果没有实现过接口,则通过CGLib动态代理生成代理类,并创建对象
边栏推荐
- 荷兰蒂尔堡大学、联邦大学 | Neural Data-to-Text Generation Based on Small Datasets: Comparing the Added Value of Two Semi-Supervised Learning Approaches on Top of a Large Language Model(基于小数据集的神经数据到文本生成)
- OneNote代码高亮
- RS2022/云检测:考虑域偏移问题的卫星图像半监督云检测Semi-Supervised Cloud Detection in Satellite Images by Considering the
- 二叉查找树的性质和用法
- VS2017#include 'xxx.h'
- 记一次 .NET 某电厂Web系统 内存泄漏分析
- Intel IPU
- Deploy eshopondapr on Tencent cloud eks
- Properties and traversal of binary trees
- 技术峰会|58同城刘元受邀参加QECON 2022全球软件质量&效能大会
猜你喜欢

Pat grade a a1086 tree traversals again

【SQL注入】堆叠注入

一种嵌入式中应用层与硬件层分层管理方法

Quickly deploy mqtt clusters on Alibaba cloud using terraform

巴比特 | 元宇宙每日必读:腾讯新闻暂停数字藏品的售卖服务,用户留言要求“退款”,幻核也陷入“滞销”困境,这释放了什么信息?...

工业以太网交换机的产品性能有哪些呢?

CodeBlocks download installation tutorial (complete and detailed)

OpenAI发文介绍Dall·E 2最新应用情况:全面进入艺术创作和设计领域

BLOOM模型背后的技术实践:1760亿参数模型如何炼成?

eureka server剖析
随机推荐
LM-Nav:基于语言、视觉和行为大模型的机器人导航方法
Fungible DPU
Why are the prices of industrial switches high and low?
浏览器兼容性测试系统以及方法和过程
音视频 SDP 添加码率
机器学习练习 5 - 偏差和方差
GPU distributed training
使用定时器触发类型处理数据库数据,函数资源使用量中这个执行时间是怎么算的?
LeNet
Top level makefile analysis of u-boot (2) config Generation of MK file
MySQL时区问题
PAT 甲级 1090 Highest Price in Supply Chain
SSM框架+jsp实现的实验室管理系统【源码+数据库+系统论文】
Is it safe to open Huatai Securities account by mobile phone?
理解了Dowanward API的妙用,轻松拿捏kubernetes环境变量
角色授权---通过添加和删除一级菜单,完成二级菜单的添加和删除
工业以太网交换机的产品性能有哪些呢?
Pat grade a 1106 lowest price in supply chain
荷兰蒂尔堡大学、联邦大学 | Neural Data-to-Text Generation Based on Small Datasets: Comparing the Added Value of Two Semi-Supervised Learning Approaches on Top of a Large Language Model(基于小数据集的神经数据到文本生成)
Create a list, add the strings "a", "B", "C", "d" and "d" in turn, and print the contents of the set, then remove all the strings "d" in the list, and print its contents again