JDK dynamic agent realizes simple AOP — conversion

JDK dynamic proxy is an important feature of java reflection. In some way, it provides dynamic characteristics for Java and brings infinite space to applications. The famous Hessian and spring AOP are implemented based on dynamic proxy. This article will briefly introduce the use of JDK dynamic agent.

About proxy mode

Agent pattern is a very common design pattern, which is often used in our applications. The general scenario is that we have a ready-made class with perfect functions, but there are still some deficiencies. At this time, we need to expand some new functions, but we don't want to rebuild the wheel. At this time, we can use the proxy class to replace the original target class and add one to add some additional functions for the target class through the combined mode.

The class structure diagram of agent mode is generally as follows:

Figure 1: proxy pattern class structure

Figure 2: agent pattern sequence diagram

JDK dynamic agent

The so-called JDK dynamic proxy is the process of dynamically generating red proxy classes at runtime. The difference between static proxy and static proxy is that static proxy needs to complete the implementation of proxy class at compile time. So how is dynamic proxy implemented?

According to the following example, look at it step by step.

Suppose there is such an interface speak:

Implementation class peoplespeak

  1. package proxy;

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>