当前位置:网站首页>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();
}
}
边栏推荐
- [leetcode]Search for a Range
- Code debugging core step memory
- Redis入门完整教程:问题定位与优化
- Increase 900w+ playback in 1 month! Summarize 2 new trends of top flow qiafan in station B
- 实施MES管理系统时,哪些管理点是需要注意的
- INS/GPS组合导航类型简介
- Error in fasterxml tostringserializerbase
- How to write test cases for test coupons?
- MySQL - common functions - string functions
- Redis入门完整教程:客户端常见异常
猜你喜欢
随机推荐
Pioneer of Web3: virtual human
Redis入门完整教程:AOF持久化
一本揭秘字节万台节点ClickHouse背后技术实现的白皮书来了!
Qt蓝牙:QBluetoothDeviceInfo
CDB PDB user rights management
Go swagger use
MySQL
Convert widerperson dataset to Yolo format
Redis入门完整教程:复制原理
你不可不知道的Selenium 8种元素定位方法,简单且实用
电气工程及其自动化
Common fitting models and application methods of PCL
换个姿势做运维!GOPS 2022 · 深圳站精彩内容抢先看!
Software testing -- common assertions of JMeter interface testing
数字滚动增加效果
A complete tutorial for getting started with redis: RDB persistence
Rethinking of investment
Huitong programming introductory course - 2A breakthrough
PSINS中19维组合导航模块sinsgps详解(滤波部分)
导数、偏导数、方向导数