search
HomeJavajavaTutorialWhat is proxy mode in java

What is proxy mode in java

May 28, 2019 pm 01:10 PM
java

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.

What is proxy mode in java

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!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Why can't the main class be found after copying and pasting the package in IDEA? Is there any solution?Why can't the main class be found after copying and pasting the package in IDEA? Is there any solution?Apr 19, 2025 pm 07:57 PM

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

Java multi-interface call: How to ensure that interface A is executed before interface B is executed?Java multi-interface call: How to ensure that interface A is executed before interface B is executed?Apr 19, 2025 pm 07:54 PM

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...

In Java programming, how to stop subsequent code execution when student ID is repeated?In Java programming, how to stop subsequent code execution when student ID is repeated?Apr 19, 2025 pm 07:51 PM

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,...

Ultimate consistency: What business scenarios are applicable to? How to ensure the consistency of the final data?Ultimate consistency: What business scenarios are applicable to? How to ensure the consistency of the final data?Apr 19, 2025 pm 07:48 PM

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...

After the Spring Boot service is running for a period of time, how to troubleshoot?After the Spring Boot service is running for a period of time, how to troubleshoot?Apr 19, 2025 pm 07:45 PM

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 the video stream of Hikvision camera SDK to the front-end Vue project for real-time playback?How to push the video stream of Hikvision camera SDK to the front-end Vue project for real-time playback?Apr 19, 2025 pm 07:42 PM

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 limit access to access_token via OAuth2.0 scope parameter?How to limit access to access_token via OAuth2.0 scope parameter?Apr 19, 2025 pm 07:39 PM

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

In Spring Boot Redis, how to solve the problem of returning garbled codes?In Spring Boot Redis, how to solve the problem of returning garbled codes?Apr 19, 2025 pm 07:36 PM

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

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.