Overall it is divided into:
1. Static proxy:
The proxy class and the proxy class need to implement the same interface. Initialize the proxy class object in the proxy class. Call the method of the proxy class in the method of the proxy class. You can selectively add functions or control access before and after the method is executed.
2. Dynamic proxy:
During program execution, JDK's reflection mechanism is used to create proxy objects and dynamically specify the objects to be proxied. Dynamic proxies do not require the creation of proxy classes.
Dynamic proxy is the ability to create java objects
In java, you want to create an object:
1. Through the construction method new object
2.Clone
3.Reflection
4.Deserialization
2.1.JDK dynamic proxy: JDK proxy is an interface-based proxy that requires the target object to implement at least one interface. JDK dynamic proxy is a proxy mechanism provided by the Java standard library, which generates proxy objects based on interfaces. JDK dynamic proxy uses java.lang.reflect.Proxy
classes andInvocationHandler
interfaces to create proxy objects, and requires that the target object must implement at least one interface. This proxy mechanism is suitable for interface proxy, usually used in AOP and other scenarios.
2.2. CGLIB dynamic proxy :MethodInterceptor
It is part of the CGLIB proxy mechanism and is usually used together with the CGLIB proxy. CGLIB dynamic proxy is an independent bytecode generation library that creates proxy objects by generating subclasses of the target object and does not require the target object to implement the interface. CGLIB dynamic proxies can proxy ordinary classes, not just classes that implement interfaces. This proxy mechanism is suitable for class proxies and is usually used in@Transactional
scenarios such as the ORM framework Spring.
2.3.javassist dynamic proxy: Javassist is another bytecode manipulation library that can be used to generate dynamic proxy classes. Javassist dynamic proxy is similar to CGLIB and can proxy ordinary classes, but its usage and implementation are slightly different.
2.4. Byte Buddy dynamic proxy: Byte Buddy is a modern bytecode generation library that can also be used to create dynamic proxy objects. Byte Buddy provides a more intuitive and powerful API that can be used to generate proxy classes.
The role of using proxy mode:
1. Function enhancement: Add additional functions before and after the original functions
2. Access control: for example, setting conditions so that access can only be done if the conditions permit.
1. Static proxy: In the implementation of static proxy, the proxy class and the proxy class must implement the same interface, and the proxy class needs to be implemented manually and the target class you want to proxy is determined.
Simple to implement and easy to understand
New Project
Set maven and encoding
New moudle
First learn how to call invoke() using Method
Example
Logs are added before and after the method, but for the HelloSpeaker class, logging is not business logic and will add extra burden.
If this kind of log needs to be used everywhere in the program, the programmer has to write log actions everywhere.
Is there a way for programmers to modify simple codes so that they can selectively log?
Define an interface HelloSpeaker1 like the original HelloSpeaker
Now write a static proxy class and do this
The test results are exactly the same as before
Then if I need to log, I only need to modify new HelloProxy(new HelloSpeaker1);
If I don’t need to log, I will use new HelloSpeaker();
Write it differently
Sometimes you have to learn to be more varied
For example, in the proxy class HelloProxy
Directly use new when defining member variables.
Then there are two kinds in the end
new HelloProxy();
or new HelloSpeaker();
The proxy class enhances the functionality of the proxied class and can perform access control
This is a static proxy
Static proxy requirements: the proxy class and the proxy class must implement the same interface.
Disadvantages: If there are too many target classes, the number of proxy classes may also increase exponentially, and the increase in functions in the interface will affect the target classes and
Let’s take a look at dynamic proxy
JDK dynamic proxy: the target class needs to implement a certain interface
Proxy.newProxyInstance (ClassLoad of the target class, array of interfaces implemented by the target class, processing object) Create a proxy object
The first parameter: Obtain ClassLoad: through the target object.getClass.getClassLoader()
Or target class.class.getClassLoader() or target interface.class.getClassLoader()
Anyway, it’s just a ClassLoader object
The second parameter is: Class<?>[ ] clazzs interface array class object
The third parameter is: the processor object itself
Anonymous writing
JDK dynamic proxy does not generate .class bytecode files (generated in memory)
CGLIB dynamic proxy will generate .class bytecode file
CGLIB proxy: CGLIB is a third-party tool library that dynamically creates proxy objects
The principle of CGLIB is inheritance. By inheriting the target class (that is, the proxied class), it dynamically creates its subclasses.
Rewrite the method with the same name of the parent class (target class) in the subclass to achieve functional modification (then the target class cannot be final, and the method cannot be final)
Example
Finally, let’s take a look at what the object references of the JDK proxy and CGLIB proxy look like.............
What JDK looks like
What CGLIB looks like
Function enhancement or access control without changing the original code