This is the 15th day of my participation in the August Text Challenge.More challenges in August.

👨🎓 author: Java Academic Conference

🏦 Storage: Github, Gitee

✏️ blogs: CSDN, Nuggets, InfoQ, Cloud + Community

💌 public account: Java Academic Party

🚫 special statement: the original is not easy, shall not be reproduced or copied without authorization, if you need to reproduce can contact xiaobian authorization.

🙏 Copyright notice: part of the text or pictures in the article come from the Internet and Baidu Encyclopedia, if there is infringement, please contact xiaobian as soon as possible. Wechat search public Java academic party contact xiaobian.

☠️ Daily toxic chicken Soup: The life you envy is the bitter you have not endured

👋 Hello! I am your old friend Java Academic Party and today I will share JDK dynamic Proxy (AOP) with you.

JDK Dynamic Proxy (AOP)

1. Proxy mode

  • The proxy pattern refers to providing a proxy for other objects to control access to that object. In some cases, an object is inappropriate or cannot directly reference another object, and a proxy object can act as an intermediary between the client class and the target object.

  • In other words, proxy objects are used to enhance the main business logic without modifying the target object. The object that the client class really wants to access is the target object, but the object that the client class really can access is the proxy object.

2. Function of the proxy mode

  • Control access: In a proxy, control whether the target object’s methods can be called.
  • Enhancements: You can attach an additional function to the target object upon completion of the call. These additional statements are called enhancements.

3. Classification of proxy modes

  • Static proxy: A proxy class is a Java file that is implemented manually and the target of the proxy is specified.
  • Dynamic proxy: Use reflection to create proxy class objects during program execution.

3.1 Static Proxy

  • Static proxy means that the proxy class is defined before the program runs. Java source files whose relationship to the target class is established before the program runs. The proxy class is compiled into a.class file before the program runs.

3.2 Disadvantages of static proxies

  • The code is complex and difficult to manage

    The proxy class and the target class implement the same interface, and each proxy needs to implement the methods of the target class, resulting in a lot of code duplication. If an interface adds a method, all proxy classes need to implement the method in addition to all target classes. Increases the complexity of code maintenance.

  • There are too many proxy classes depending on the target class

    A proxy class only serves a target class of one type, if multiple types are to be served. It is necessary to proxy for every target class, and static proxies cannot do the job when the program size is slightly larger, because there are too many proxy classes. When the interface changes, there are many target classes and proxy analogies that need to be changed.

3.3 Advantages of static proxy

  • Easy to understand and use.

3.4 Dynamic Management

  • Dynamic proxies are proxy class objects that are dynamically generated by the JVM based on reflection while the program is running. Dynamic proxies do not require. Java source files that define proxy classes.
  • Dynamic proxies are simply classes that are dynamically created and loaded into the JVM while the JDK is running.
  • Dynamic proxies use the JDK’s reflection mechanism, the ability to create objects, to create objects of the proxy class. Instead of using the class file you created. No need to write a Java file.

3.6 Implementation of dynamic proxy

  • JDK Dynamic Proxy (Understood) : Implement dynamic proxy functionality using classes and interfaces in the Java reflection package.

    ** Note: In the JDK proxy, the target class must have an interface, but in CGLIB the target class can have no interface.

    The java.lang.Reflect package contains three classes: InvocationHandler, Method, and ProxyCopy the code
  • CGLIB dynamic proxy:

    CGLIB(Code Generation Library) is an open source project. Is a powerful, high-performance, high-quality code-generated library that extends Java classes and implements Java interfaces at run time. It is widely used by many AOP frameworks, such as Spring AOP. Implementing proxies using JDK proxies requires the target class to implement the same interface as the Proxy class. If the target class does not have an interface, this method cannot be implemented. However, to create a dynamic proxy for a class without an interface, CGLIB is used. The CGLIB proxy is generated by generating an enhanced subclass of the target class. The subclass object is the proxy object. Therefore, using CGLIB to generate dynamic proxies requires that the target class must be inheritable, that is, not final. Cglib is often used in frameworks such as Spring, Hibernate, etc. Cglib has a higher agent efficiency than the Jdk. Cglib is not used in general development. Just do an understanding

3.5 Advantages of dynamic proxy

  • Instead of creating a proxy class file, the proxy’s target class is active and configurable.
  • Agents can be created dynamically for different targets.

JDK dynamic proxy

  • Reflection: Method class, representing methods. A Method that executes a Method in a class.

4.1 InvocationHandler interface

4.2 Method class

4.3 the Proxy class

This Object is now equivalent to the static proxy Object Taobao

5.JDK implementation of dynamic proxy steps

6.JDK dynamic proxy execution process

7. What can dynamic code do

——-💘 after watching the big guys can pay attention to the xiaobian, will always update the small skills, free to share with you!! 💝—– today first share here, tomorrow to continue to share with you, pay attention not lost yo, we will see 😊 tomorrow.