One, static agent and dynamic proxy comparison:
1, static agent writing requirements:
1) The decorator and decorator need to implement the same interface or implement the same class.
2) The decorator should have a reference to the decorator.
3) Strengthened methods are needed.
4) Do not need to strengthen the method, the implementation of the original method.
2. Dynamic Agent Method
When the project is running, generate an object for the agent to enhance the method.
2 different ways:
1) The method proxy class in the JDK, provided that the interface is implemented.
2) Spring in Cglib, premise: Inherit class.
Dynamically creates a proxy object in memory.
One of the implementations in the JDK:
Static Object newproxyinstance (ClassLoader loader, class<?>[] interfaces, Invocationhandler h)
Parameter description:
1) ClassLoader Loader: Is the class loader that the proxy object loads the memory. Typically, the class loader for the Proxied object is used.
2) class<?>[] Interfaces: A collection of interfaces that a proxy object needs to implement, typically a collection of interfaces implemented by the Proxied object. 、
3) Invocationhandler: the implementation of the processing class, here on the need to strengthen the method, to strengthen.
Invocationhandler:interface Invocationhandler. He is an interface, and we can implement that interface through anonymous classes.
Method: Object Invoke (Object proxy, Method method, object[] args)
Parameter description:
Proxy: Agent object.
Method: The currently executing methods.
Object[] args: The parameters required for the currently executing method.
Java Dynamic Agent