First, the agent
Provides a proxy for other objects to control access to this object.
(1) Static agent
(2) Dynamic agent
1) The JDK's own dynamic agent
2) Javaassist byte code operation library Implementation
3) CGLIB
4) ASM (lower level instruction, poor maintainability)
Second, static agent
A static proxy mode typically has three roles:
Abstract role: A public method that is provided externally by the agent role and the real role, typically an interface
Real role: You need to implement an abstract role interface that defines the business logic to be implemented by the real role for the proxy role to invoke. That is the real business logic here.
Proxy role: the need to implement an abstract role interface, is the real role of the agent, through the real role of the business logic method to implement the abstract method, and can attach their own operations.
Third, dynamic agent (1) Proxy
Invocationhandler interface
. Invoke (Object Proxy,method moehtod,object[] args) Proxy execution interface interception method
Proxy
. Newproxyinstance (ClassLoader loader,class[] Interfaces,invcationhandler handler) Creating a dynamic proxy instance
(2) CGLIB
Methodinterceptor
- Intercept (Object Obj,method method,object[] Args,methodproxy proxy) Object interception method
Enhancer
- Setsuperclass (class superclass) Set parent class
- Setcallback (CallBack CallBack) set callback blocker
- Setclassloader (ClassLoader loader)
- Create () object creates a proxy object
Methodproxy
- Invokesuper (Object obj,object[] args) object executes proxy object
- Invoke (Object target,object[] args) object to execute the proxied objects
Java Agent Summary