java reflection and proxy mode

 

 

What is the reflection mechanism

The reflection mechanism is that in the running state, for any class, you can know all the properties and methods of this class; for any object, you can call any of its methods and properties; this kind of dynamically obtained information and dynamic calling objects The function of the method is called the reflection mechanism of the java language.

What the reflection mechanism can do

The reflection mechanism mainly provides the following functions:

  • Determine the class to which any object belongs at runtime;
  • Construct an object of any class at runtime;
  • Determine the member variables and methods of any class at runtime;
  • call a method of any object at runtime;
  • Generate dynamic proxies.

 

1. Proxy mode

Proxy (Proxy) is a design pattern that provides another way of accessing the target object; that is, accessing the target object through the proxy object. The advantage of this is that additional functional operations can be enhanced based on the implementation of the target object, namely Extend the function of the target object.
Here is an idea in programming: do not arbitrarily modify the code or methods that have been written by others. If you need to modify it, you can extend the method by proxy

Give an example to illustrate the role of an agent: Suppose we want to invite a star, then instead of directly connecting with the star, we will contact the star's agent to achieve the same purpose. The star is a target, and he only needs to be responsible for the activities in the event. show, while other trivial matters are left to his agent (agent) to solve. This is an example of agency thinking in reality

 

 

Static proxy, dynamic proxy: JDK dynamic proxy, CGLIB

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326266965&siteId=291194637