当前位置:网站首页>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();
}
}
边栏推荐
- The 8 element positioning methods of selenium that you have to know are simple and practical
- PCL 常用拟合模型及使用方法
- [leetcode]Search for a Range
- Redis入门完整教程:问题定位与优化
- Huitong programming introductory course - 2A breakthrough
- The so-called consumer Internet only matches and connects industry information, and does not change the industry itself
- 从零安装Redis
- Introduction to ins/gps integrated navigation type
- C language exercises_ one
- Examples of how to use dates in Oracle
猜你喜欢
一文读懂Faster RCNN
【Socket】①Socket技术概述
Redis入門完整教程:問題定比特與優化
Redis入门完整教程:复制原理
What are the characteristics of the operation and maintenance management system
The annual salary of general test is 15W, and the annual salary of test and development is 30w+. What is the difference between the two?
Planning and design of double click hot standby layer 2 network based on ENSP firewall
Cloud Mail .NET Edition
运维管理系统有哪些特色
Go swagger use
随机推荐
Static proxy of proxy mode
Redis入门完整教程:复制拓扑
unity中跟随鼠标浮动的面板,并可以自适应文字内容的大小
服装企业部署MES管理系统的五个原因
Detailed explanation of 19 dimensional integrated navigation module sinsgps in psins (time synchronization part)
wireshark安装
测试优惠券要怎么写测试用例?
MySQL
一文读懂Faster RCNN
Kubernetes源码分析(二)----资源Resource
Apifox, is your API interface document rolled up like this?
ERROR: Could not find a version that satisfies the requirement xxxxx (from versions: none)解决办法
fasterxml ToStringSerializerBase报错
左程云 递归+动态规划
Difference and the difference between array and array structure and linked list
3 -- Xintang nuc980 kernel supports JFFS2, JFFS2 file system production, kernel mount JFFS2, uboot network port settings, and uboot supports TFTP
Ali yunyili: how does yunyuansheng solve the problem of reducing costs and improving efficiency?
PSINS中19维组合导航模块sinsgps详解(初始赋值部分)
Redis入门完整教程:复制配置
用全连接+softmax对图片的feature进行分类