We all know that the two main features of spring are IOC and AOP.
IOC is responsible for the dynamic injection of the object into the container, so as to achieve a need to inject who who, when the need to inject the effect of the time, can be described as a recruit, the lingering. Think all feel cool, if the real life also has this ability that is cool crooked, as for how cool, you own brain repair it, and AOP, it is to achieve the container is another great advantage, that is, you can let the objects in the container to enjoy the public services in the container. So how did the container do it? How can it let the object in it automatically have the public service it provides? The answer is what we're going to talk about today--dynamic proxies.
Dynamic agent is not something new, the person who has learned the design pattern should know the proxy mode, the proxy mode is a static proxy, and the dynamic agent is to use reflection and dynamic compilation to turn the proxy mode into dynamic. The principle is the same as the dynamic injection, the proxy mode at compile time has determined that the proxy class will be agent who, and the dynamic agent in the run time only know who to proxy.
There are two types of dynamic agents in spring: One is the dynamic agent of the JDK, and the other is the Cglib dynamic proxy (which implements the proxy by modifying the bytecode). Today we mainly discuss the way of the JDK dynamic proxy. The main way to do this is through reflection and dynamic compilation, so let's look at the code to see how it is implemented.
The implementation principle of Spring container AOP--Dynamic agent