Home> Java> javaTutorial> body text

What is proxy mode in java

(*-*)浩
Release: 2019-05-28 13:10:23
Original
4006 people have browsed it

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!

Related labels:
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!