当前位置:网站首页>Cglib agent in agent mode
Cglib agent in agent mode
2022-07-07 02:50:00 【Xiaobai without work】
1:CGLIB Agency features
cglib It's about classes that implement proxies
Its principle is to generate a subclass of the specified target class , And cover the methods to realize the enhancement
Because it's inheritance , So we can't be right final Decorated class to proxy
2: Code implementation
2.1: stay pom.xml Introduce in the file cglib Related dependence of
<!-- https://mvnrepository.com/artifact/cglib/cglib -->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>
</dependency>
2.2: Define the target class
// You don't need to write a parent class or implement an interface
public class Me {
public void show(){
System.out.println(" I did ");
}
}
2.3: Define proxy class , Implementation interface
/* cglib A dynamic proxy */
public class CglibInterceptor implements MethodInterceptor {
// Target audience
private Object target;
// Pass in the target object through the constructor
public CglibInterceptor(Object target) {
this.target = target;
}
/*** Interceptor * 1、 Method call of target object * 2、 Reinforcement behavior * @param o from CGLib Dynamically generated proxy class instances * @param method The proxy method reference called by the entity class * @param objects Parameter value list * @param methodProxy The proxy reference of the generated proxy class to the method * @return * @throws Throwable */
@Override
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
System.out.println(" Before method execution ...");
Object result = methodProxy.invoke(target, objects);
System.out.println(" After method execution ...");
return result;
}
public Object getProxy(){
// adopt Enhancer Object's create() Method can generate a class , Used to generate proxy objects
Enhancer enhancer = new Enhancer();
// Set parent class ( No parent class has the target class as its parent )
enhancer.setSuperclass(target.getClass());
// Set up interceptors The callback object is its own object
enhancer.setCallback(this);
// Generate a proxy class object , And back to
return enhancer.create();
}
}
2.4: Code testing
public class Test {
public static void main(String[] args) {
Me me = new Me();
CglibInterceptor cglib02 = new CglibInterceptor(me);
Me u = (Me) cglib02.getProxy();
u.show();
}
}
边栏推荐
猜你喜欢
Classify the features of pictures with full connection +softmax
你不可不知道的Selenium 8种元素定位方法,简单且实用
普通测试年薪15w,测试开发年薪30w+,二者差距在哪?
HAVE FUN | “飞船计划”活动最新进展
Five reasons for clothing enterprises to deploy MES management system
AWS学习笔记(一)
unity中跟随鼠标浮动的面板,并可以自适应文字内容的大小
Cloud Mail .NET Edition
Digital scrolling increases effect
换个姿势做运维!GOPS 2022 · 深圳站精彩内容抢先看!
随机推荐
Qpushbutton- "function refinement"
Kubernetes源码分析(二)----资源Resource
Mmdetection3d loads millimeter wave radar data
[node learning notes] the chokidar module realizes file monitoring
Qt蓝牙:QBluetoothDeviceInfo
Niuke programming problem -- double pointer of 101 must be brushed
3 -- Xintang nuc980 kernel supports JFFS2, JFFS2 file system production, kernel mount JFFS2, uboot network port settings, and uboot supports TFTP
基于ensp防火墙双击热备二层网络规划与设计
AWS learning notes (I)
Static proxy of proxy mode
KYSL 海康摄像头 8247 h9 isapi测试
安全交付工程师
差异与阵列和阵列结构和链表的区别
巴比特 | 元宇宙每日必读:IP授权是NFT的破圈之路吗?它的难点在哪里?Holder该如何选择合作平台?...
Electrical engineering and automation
Common fitting models and application methods of PCL
Lombok makes the pit of ⽤ @data and @builder at the same time
Contribution of Writing Series
Apifox,你的API接口文档卷成这样了吗?
dotConnect for DB2数据提供者