当前位置:网站首页>And play the little chestnut of dynamic agent
And play the little chestnut of dynamic agent
2022-07-05 07:16:00 【I am the king of X】
Go straight to the code
List of articles
Premise
This needs to be understood in combination with another blog post :
Code
Interface :
package com.xmonster.demo2;
public interface IService {
public void sayHello();
}
Real implementation class :
package com.xmonster.demo2;
public class RealService implements IService{
@Override
public void sayHello() {
System.out.println("hello i am xmonster!");
}
}
Tools + test :
package com.xmonster.demo2;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class SimpleInvocationHandler implements InvocationHandler {
private Object realObj;
public SimpleInvocationHandler(Object realObj) {
this.realObj = realObj;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("entering"+method.getName());
Object invokeResult = method.invoke(realObj, args);
System.out.println("leaving"+method.getName());
return invokeResult;
}
public static void main(String[] args) {
IService realService = new RealService();
IService proxy =(IService) Proxy.newProxyInstance(IService.class.getClassLoader(), new Class<?>[]{
IService.class},
new SimpleInvocationHandler(realService));
proxy.sayHello();
}
}
test result :
newProxyInstance
Let's see newProxyInstance Three parameters of
private static Object newProxyInstance(ClassLoader loader, // null if no SecurityManager
Constructor<?>[] interfaces,
InvocationHandler h)
Parameters :
- ClassLoader loader Represents a class loader , Use the same class loader as the interface here
- interfaces Represents the list of interfaces to be implemented by the proxy class , Is an array ,
The type of element can only be interface , It cannot be a general class
- InvocationHandler It's an interface , Only one... Is defined invoke Method , Calls to all methods of the proxy interface will be transferred to this method for processing
newProxyInstance The return value of Object, Can be forced to interfaces An interface type in the array
, Here, it is directly converted to IService type , Be careful : It cannot be forcibly converted to a class , such as RealService !
InvocationHandler
package java.lang.reflect;
public interface InvocationHandler {
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable;
}
Parameters :
- proxy Represents the agent itself , Be careful , He is not the object of being represented
- method Represents the method being called
- args Parameters representing methods
Object invokeResult = method.invoke(realObj, args);
So this is calling theta method Of invoke Method , What is passed is the actual object realObj
, Can't be proxy Pass as parameter to method.invoke, There will be a dead cycle , Because it has this method itself
Here are method Of invoke Method source code
@HotSpotIntrinsicCandidate
public Object invoke(Object obj, Object... args)
throws IllegalAccessException, IllegalArgumentException,
InvocationTargetException
{
if (!override) {
Class<?> caller = Reflection.getCallerClass();
checkAccess(caller, clazz,
Modifier.isStatic(modifiers) ? null : obj.getClass(),
modifiers);
}
MethodAccessor ma = methodAccessor; // read volatile
if (ma == null) {
ma = acquireMethodAccessor();
}
return ma.invoke(obj, args);
}
边栏推荐
- 数学分析_笔记_第8章:重积分
- IPage能正常显示数据,但是total一直等于0
- How can Oracle SQL statements modify fields that are not allowed to be null to allow nulls?
- SD_ CMD_ RECEIVE_ SHIFT_ REGISTER
- 能量守恒和打造能量缺口
- 纯碱是做什么的?
- golang定时器使用踩的坑:定时器每天执行一次
- An article was opened to test the real situation of outsourcing companies
- 【idea】Could not autowire. No beans of xxx type found
- [vscode] search using regular expressions
猜你喜欢
Anaconda navigator click open no response, can not start error prompt attributeerror: 'STR' object has no attribute 'get‘
[software testing] 03 -- overview of software testing
SD_CMD_SEND_SHIFT_REGISTER
Pytorch has been installed in anaconda, and pycharm normally runs code, but vs code displays no module named 'torch‘
逻辑结构与物理结构
【Node】nvm 版本管理工具
第 2 章:小试牛刀,实现一个简单的Bean容器
[vscode] prohibit the pylance plug-in from automatically adding import
C语言数组专题训练
C learning notes
随机推荐
【软件测试】05 -- 软件测试的原则
Docker installs MySQL and uses Navicat to connect
SD_ CMD_ SEND_ SHIFT_ REGISTER
Do you choose pandas or SQL for the top 1 of data analysis in your mind?
PHY drive commissioning - phy controller drive (II)
[untitled]
GPIO port bit based on Cortex-M3 and M4 with operation macro definition (can be used for bus input and output, STM32, aducm4050, etc.)
[tf1] save and load parameters
Target detection series - detailed explanation of the principle of fast r-cnn
Steps and FAQs of connecting windows Navicat to Alibaba cloud server MySQL
【obs】x264编码:“buffer_size“
Ros2 - first acquaintance with ros2 (I)
[framework] multi learner
SD_CMD_RECEIVE_SHIFT_REGISTER
U-boot initialization and workflow analysis
M2DGR 多源多场景 地面机器人SLAM数据集
【软件测试】03 -- 软件测试概述
golang定时器使用踩的坑:定时器每天执行一次
postmessage通信
二分查找(折半查找)