当前位置:网站首页>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();
}
}
边栏推荐
- dotConnect for DB2数据提供者
- Fundamentals of process management
- What management points should be paid attention to when implementing MES management system
- [leetcode]Search for a Range
- Safety delivery engineer
- Five reasons for clothing enterprises to deploy MES management system
- 代码调试core-踩内存
- 巴比特 | 元宇宙每日必读:IP授权是NFT的破圈之路吗?它的难点在哪里?Holder该如何选择合作平台?...
- Redis getting started complete tutorial: common exceptions on the client
- Qpushbutton- "function refinement"
猜你喜欢
The third season of ape table school is about to launch, opening a new vision for developers under the wave of going to sea
Redis入门完整教程:复制配置
LeetCode 77:组合
你不可不知道的Selenium 8种元素定位方法,简单且实用
Derivative, partial derivative, directional derivative
MySQL is an optimization artifact to improve the efficiency of massive data query
Go swagger use
Apifox,你的API接口文档卷成这样了吗?
Mmdetection3d loads millimeter wave radar data
Read fast RCNN in one article
随机推荐
Redis getting started complete tutorial: common exceptions on the client
Derivative, partial derivative, directional derivative
The third season of ape table school is about to launch, opening a new vision for developers under the wave of going to sea
LeetCode 77:组合
Code debugging core step memory
安全巡检的工作
CDB PDB user rights management
Read fast RCNN in one article
Examples of how to use dates in Oracle
Metaforce force meta universe fossage 2.0 smart contract system development (source code deployment)
数论 --- 快速幂、快速幂求逆元
慧通编程入门课程 - 2A闯关
[software test] the most complete interview questions and answers. I'm familiar with the full text. If I don't win the offer, I'll lose
Contribution of Writing Series
STM32 project -- Topic sharing (part)
Planning and design of double click hot standby layer 2 network based on ENSP firewall
MySQL提升大量数据查询效率的优化神器
代码调试core-踩内存
Google Earth engine (GEE) -- 1975 dataset of Landsat global land survey
Number theory --- fast power, fast power inverse element