当前位置:网站首页>CGLIB动态代理
CGLIB动态代理
2022-06-25 06:39:00 【Borny鼎鼎】
1、说明
动态生成一个要代理类的子类,子类重写要代理的类的所有不是final的方法。为没有实现接口的类提供代理,是jdk动态代理很好的补充。应用场景,例如aop、hibernate。
2、简单实例
public class DogService {
public void sound() {
System.out.println("wang");
}
}
DogService dogService = new DogService();
// cglib动态代理
Enhancer enhancer = new Enhancer();
enhancer.setClassLoader(dogService.getClass().getClassLoader());
enhancer.setSuperclass(dogService.getClass());
MethodInterceptor methodInterceptor = new MethodInterceptor() {
@Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy)
throws Throwable {
System.out.println("before");
Object invoke = method.invoke(dogService, args);
System.out.println("after");
return invoke;
}
};
enhancer.setCallback(methodInterceptor);
DogService dogServiceProxy = (DogService) enhancer.create();
dogServiceProxy.sound();
测试结果
before
wang
after
边栏推荐
- [tool sharing] a software that pays equal attention to appearance and skills
- Why use NoSQL with MySQL?
- Orcad Schematic常用功能
- lotus v1.16.0-rc2 Calibration-net
- 诸葛亮 VS 庞统,拿下分布式 Paxos
- Why is true == "true" true in R- Why TRUE == “TRUE” is TRUE in R?
- Lotus windowsost manually triggers space-time proof calculation
- 【批处理DOS-CMD命令-汇总和小结】-cmd扩展命令、扩展功能(cmd /e:on、cmd /e:off)
- Finally, when you open source the applet ~
- Is it possible to use Jasmine's toHaveBeenCalledWith matcher with a regular expression?
猜你喜欢
我们不一样
Cocos learning diary 3 - API acquisition nodes and components
Classic paper in the field of character recognition: aster
My debut is finished!
Why use NoSQL with MySQL?
What common APIs are involved in thread state changes
ES 终于可以搜到”悟空哥“了!
【UVM入门 ===> Episode_9 】~ 寄存器模型、寄存器模型的集成、寄存器模型的常规方法、寄存器模型的应用场景
单片机IO详解(上拉 下拉 准双向 输入 输出 推挽 开漏)
48 pictures | teach you the performance monitoring, pressure testing and tuning of microservices by hand
随机推荐
keepalived監控進程,自動重啟服務進程
LTpowerCAD II和LTpowerPlanner III
Streamnational platform version 1.5 is released, integrating istio and supporting openshift deployment
48 张图 | 手摸手教你微服务的性能监控、压测和调优
Classic paper in the field of character recognition: aster
Why is true == "true" true in R- Why TRUE == “TRUE” is TRUE in R?
Enter an integer with any number of bits, and output the sum of each bit of the number. For example: 1234 – > 10
100 times larger than the Milky way, Dutch astronomers found mysterious objects in deep space
【C语言】给字符串增加分隔符
313. Binary sum
How to get the difference between two dates rounded to hours
lotus windowPoSt 手动触发时空证明计算
lotus v1.16.0-rc3 calibnet
用太极拳讲分布式理论,真舒服!
【批處理DOS-CMD命令-匯總和小結】-cmd擴展命令、擴展功能(cmd /e:on、cmd /e:off)
Advanced mathematics foundation_ Parity of functions
[tool sharing] a software that pays equal attention to appearance and skills
【xxl-job】池塘水绿风微暖,记得玉真初见面
[C language] add separator to string
【批处理DOS-CMD命令-汇总和小结】-上网和网络通信相关命令(ping、telnet、nslookup、arp、tracert、ipconfig)