当前位置:网站首页>Dynamic proxy
Dynamic proxy
2022-07-01 08:40:00 【SXHENIGA】
One 、 What is agency
sketch : Buying on behalf of 、 The mediation 、 ladder 、 Businesses and so on
characteristic : The agent and the target should do the same thing
Two 、 The proxy pattern
sketch : Agency mode means , Provides a proxy for other objects to control access to this object . In some cases , An object Not suitable or cannot be quoted directly Another object , The proxy object can be in Customer class and Target audience Play a role in The mediation The role of . In short : The target object is not accessible , Access... Through proxy object enhancements ;
effect : Enhancements 【 Add extra features 】; Control access to the target object ;
classification : Static proxy 【① The proxy class is implemented manually ② The target class needs to be certain 】; A dynamic proxy ;
3、 ... and 、 Static proxy
characteristic : The target object and the proxy object implement the same business interface ; The target object must implement the interface 【 Compared with dynamic proxy 】; The proxy object already exists before the program runs ; It can flexibly switch the target objects , But it can't be function Flexible handling of
shortcoming : In static proxy , Acting The function is dead , Unable to change dynamically , And this function already exists when the project starts ; If you want to add the function of agent , It needs to be in the interface 、 Target class and proxy class Add new code between , The impact is more , Dynamic proxy can automatically generate proxy classes .
Realization :
0、 Project directory
2、 Create factory class 【 Target class 】, Implementation steps 1 The interface of 3、 Create a merchant , It's the agent , You also need to implement steps 1 The interface of 4、 Create a client class , Call the merchant's method to buy something |
Four 、 A dynamic proxy
sketch : Proxy objects are dynamically built in memory during program execution , It can be carried out flexibly Business function Of Switch . let me put it another way : When the program is running, the proxy class object JVM Generated dynamically according to the reflection mechanism , There is no need to define a proxy class .java object
1、JDK Dynamic proxy for :
sketch : Use java The classes and interfaces in the reflection package realize the function of dynamic agent
characteristic : The target object must implement the business interface ;JDK Proxy objects do not need to implement business interfaces ;JDK The object of dynamic proxy does not exist before the program runs , Dynamically build in memory while the program is running ;JDK Dynamic agent can flexibly switch business functions ; Methods in this class ( Methods in non interface ) Can't be represented ; You can enhance your functional code in the agent
Parameters : It is done using tool classes JDK Dynamic implementation of , Reflex package java.lang.reflect
![]() |
1)Proxy class : It is java.lang.reflect.Proxy Class under package , It has a way Proxy.newProxyInstance{....}, Specifically used to generate dynamic proxy objects
2)Method class : Class for reflection , Used to make reflection calls to target object methods ,Method Object to catch the method we are calling
3)InvocationHandler Interface : Used to implement agent and business functions , We use anonymous internal implementation when calling , We wrote it ourselves
Realization :
0、 Project structure 1、 Create an interface , Define the functions to be completed by the target class 2、 Create the implementation interface of the target class 3、 establish InvocationHandler Implementation class of interface , stay invoke Method to complete the function of the proxy class 1) Calling the target method 2) Enhancements 4、 Use Proxy Class static methods , Create proxy object , And convert the return value to the interface type .
Actually : Dynamic agents are better than static agents , Namely : Automatically add the enhanced code to the front or back of the method , You don't have to write enhanced code for every method ; But static proxy can only add enhanced code one method at a time , Although the enhanced code is the same . |
2、CGLib A dynamic proxy :
sketch : Referred to as subclass dynamic proxy , By dynamically building subclass objects in memory , Override the method of the parent class to enhance the proxy function . If the target object does not implement the interface , Only through CGLib Subclass proxy for function enhancement . Subclass proxies are object bytecode frameworks ASM To achieve ;CGLib stay MyBatis and Spring In the framework
Be careful : The represented class cannot be final, Because it's inheritance
边栏推荐
猜你喜欢
Brief introduction to AES
毕业论文中word的使用1-代码域标公式
VSYNC+三重缓存机制+Choreographer
Field agricultural irrigation system
Centos7 shell脚本一键安装jdk、mongo、kafka、ftp、postgresql、postgis、pgrouting
What is the material of 16mo3 steel plate? What is the difference between 16mo3 and Q345R?
【js逆向】md5加密参数破解
It is designed with high bandwidth, which is almost processed into an open circuit?
There are many problems in sewage treatment, and the automatic control system of pump station is solved in this way
Matlab [function derivation]
随机推荐
Audio audiorecord create (I)
Introduction to R language
MAVROS发送自定义话题消息给PX4
[no title] free test questions for constructor municipal direction general foundation (constructor) and theoretical test for constructor municipal direction general foundation (constructor) in 2022
内存大小端
嵌入式工程师面试-常问问题集
V79.01 Hongmeng kernel source code analysis (user mode locking) | how to use the fast lock futex (Part 1) | hundreds of blogs analyze the openharmony source code
AVL树的理解和实现
Computer tips
What is the material of 15CrMoR, mechanical properties and chemical analysis of 15CrMoR
19Mn6 German standard pressure vessel steel plate 19Mn6 Wugang fixed binding 19Mn6 chemical composition
Introduction to 18mnmo4-5 steel plate executive standard and delivery status of 18mnmo4-5 steel plate, European standard steel plate 18mnmo4-5 fixed rolling
1. Connection between Jetson and camera
《MATLAB 神经网络43个案例分析》:第30章 基于随机森林思想的组合分类器设计——乳腺癌诊断
Tita OKR: a dashboard to master the big picture
SPL Introduction (I)
NFT监管要点和海外政策
Only in China! Alicloud container service enters the Forrester leader quadrant
【js逆向】md5加密参数破解
Yolov3, 4, 5 and 6 Summary of target detection