Proxy is a design pattern that provides another way to access the target object; that is, access the target object through the proxy object. The advantage of this is that additional functions can be enhanced based on the implementation of the target object. Operation, that is, extending the function of the target object.
An idea in programming is used here: do not modify the code or methods that others have written at will. If If you need to modify it, you can extend this method through an agent.
Take an example to illustrate the role of an agent: Suppose we want to invite a star, then we do not directly connect to the star, but contact the star's agent , to achieve the same purpose. The star is a target object. He only needs to be responsible for the program at the event, and other trivial matters are left to his agent (agent) to solve. This is an example of agency thinking in reality
The key points of the proxy mode are: proxy object and target object. The proxy object is an extension of the target object and will call the target object
Static proxy
When using a static proxy, you need to define an interface or a parent class. The proxy object and the proxy object implement the same interface or inherit the same parent class.
Here is an example to explain:
Simulate the save action, define an interface for the save action: IUserDao.java, and then the target object implements the method UserDao.java of this interface. At this time, if you use the static proxy method, you need to also implement the IUserDao interface in the proxy object (UserDaoProxy.java) .When calling, call the target object by calling the method of the proxy object.
It should be noted that the proxy object and the target object must implement the same interface, and then call the method of the target object by calling the same method
Static proxy summary:
1. The target function can be extended without modifying the function of the target object.
2.Disadvantages:
Because of the proxy The object needs to implement the same interface as the target object, so there will be many proxy classes, too many classes. At the same time, once the interface adds methods, the target object and the proxy object must be maintained.
How to solve the shortcomings of static proxies What? The answer is that you can use dynamic proxy
Dynamic proxy
Dynamic proxy has the following characteristics:
1. Proxy object, not The interface needs to be implemented
2. The generation of proxy objects is to use the API of JDK to dynamically build proxy objects in memory (we need to specify the type of interface to create proxy objects/target objects)
3. Dynamic proxy Also called: JDK proxy, interface proxy
Summary:
The proxy object does not need to implement the interface, but the target object must implement the interface, otherwise dynamic proxy cannot be used
Cglib Proxy
The above static proxy and dynamic proxy modes require the target object to be a target object that implements an interface, but sometimes the target object is just a separate object and is not implemented. For any interface, you can use a subclass of the target object to implement the proxy at this time. This method is called: Cglib proxy
Cglib proxy, also called a subclass proxy, is constructed in memory A subclass object to extend the function of the target object.
JDK's dynamic proxy has a limitation, that is, the object using the dynamic proxy must implement one or more interfaces. If you want to proxy a class that does not implement the interface, You can use Cglib to achieve this. Cglib is a powerful and high-performance code generation package that can extend java classes and implement java interfaces at runtime. It is widely used by many AOP frameworks, such as Spring AOP and synaop, to provide them with The underlying method of interception (interception) Cglib package is to convert bytecode and generate new classes by using a small and block bytecode processing framework ASM. Direct use of ASM is discouraged because it requires you to understand the JVM internal structure. The format and instruction set including the class file are all familiar.
Cglib subclass agent implementation method:
1. The jar file of cglib needs to be introduced, but the core package of Spring has already It includes Cglib function, so you can directly introduce spring-core-3.2.5.jar.
2. After introducing the function package, you can dynamically build subclasses in memory
3. The proxy class cannot be final , otherwise an error will be reported
4. If the method of the target object is final/static, it will not be intercepted, that is, the additional business methods of the target object will not be executed.
The above is the detailed content of What is proxy mode in java. For more information, please follow other related articles on the PHP Chinese website!

Why can't the main class be found after copying and pasting the package in IDEA? Using IntelliJIDEA...

State synchronization between Java multi-interface calls: How to ensure that interface A is called after it is executed? In Java development, you often encounter multiple calls...

How to stop subsequent code execution when ID is repeated in Java programming. When learning Java programming, you often encounter such a requirement: when a certain condition is met,...

In-depth discussion of final consistency: In the distributed system of application scenarios and implementation methods, ensuring data consistency has always been a major challenge for developers. This article...

The troubleshooting idea of SSH connection failure after SpringBoot service has been running for a period of time has recently encountered a problem: a Spring...

How to push video streams from Hikvision camera SDK to front-end Vue project? During the development process, you often encounter videos that need to be captured by the camera to be circulated...

How to use access_token of OAuth2.0 to restrict interface access permissions How to ensure access_token when authorizing using OAuth2.0...

SpringBootRedis gets the key garbled problem analysis using Spring...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 English version
Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.