What is the principle of dynamic agent

Proxy mode: Solve some non-business-related issues silently through proxy, such as remote, security, transactions, logs, resource shutdown,... so that application developers can only care about their own business.

Static proxy: write the code class in advance, you can write it by hand, or you can generate it with tools. The disadvantage is that each business class must have a corresponding proxy class, which is very inflexible.

Dynamic proxy: The proxy object is automatically generated at runtime. The disadvantage is that it takes extra time to generate the proxy object and call the proxy object.

JDK dynamic proxy: Based on the implementation of the java reflection mechanism, the business class that implements the interface must be used to generate proxy objects in this way. The new version has also begun to incorporate the ASM mechanism.

cglib dynamic proxy: Based on the ASM mechanism, subclasses of business classes are generated as proxy classes.

Common applications of java reflection mechanism: dynamic proxy (AOP, RPC), third-party developer expansion capabilities (Servlet container, JDBC connection), third-party component creation object (DI)...

 

 

 

Principles of reflection and dynamic proxy:

One of the biggest effects of reflection is that we can not know the type of an object at compile time, but provide the complete "package name + class name.class" at runtime to get it. Note: Not at compile time, but at runtime.

Features:

#Can determine the class of any object at runtime.

#Can construct an object of any class at runtime

#At runtime, the member variables and methods of any class can be obtained.

#Call the method of any object at runtime. Simply put, using the java reflection mechanism we can load a class whose name is known only at runtime, learn its construction method, and generate its object entity, which can be set and invoked for its fields methods.

Application scenarios:

Reflection technology is commonly used in the development of various general frameworks. Because in order to ensure the versatility of the framework, different objects and classes need to be loaded according to the configuration file, and different methods are called. At this time, reflection will be used-dynamic loading at runtime The object that needs to be loaded.

Features:

Since reflection consumes a certain amount of system resources, if you don't need to dynamically create an object, then you don't need to use reflection. In addition, the permission check can be ignored when the reflection method is called, so it may break the encapsulation and cause security problems.

 2 dynamic proxy

Provide a proxy for other objects to control access to this object. Under certain circumstances, an object is not suitable or cannot directly refer to another object, and the proxy object can act as an intermediary between the two. Proxy means that you don't need to care about who is the proxy in the implementation phase, but only specify the proxy object in the runtime phase.If you write the proxy class yourself, it is a static proxy (determinism).

Components:

(Dynamic) The agency model mainly involves three elements:

#1: abstract class interface

#2: The proxy class (the class that concretely implements the abstract interface)

#3: Dynamic proxy class: the class that actually calls the methods and properties of the proxy class

Implementation:

There are many ways to implement dynamic agents, such as the dynamic agent provided by the JDK itself, which mainly uses the reflection mechanism, and there are other implementation methods, such as the use of bytecode operation mechanisms, similar to ASM, CGLIB (based on ASM), Javassist, etc.

Example: The dynamic proxy interface InvocationHandler provided by JDK is often used to implement dynamic proxy classes.

Guess you like

Origin blog.csdn.net/A___B___C/article/details/114141858