This article brings you an introduction to the implementation methods of JAVA dynamic proxy and CGLIB mode (code examples). It has certain reference value. Friends in need can refer to it. I hope It will help you.
Dynamic proxy is a technology that uses reflection and bytecode technology to create a subclass of a specified interface or class (dynamic proxy) and its instance object during runtime. Through this technology, code can be non-invasively Enhancement (Recommended:Java Tutorial)
Proxy: Proxy is the parent class of all dynamic proxies, which provides a static method to create a dynamic proxy Class objects and instances;
InvocationHandler: Each dynamic proxy instance has an associated InvocationHandler. When a method is called on a proxy instance, the method call will be forwarded to the invoke method of InvocationHandler;
1.java dynamic proxy implementation
//java的代理模式必须有一个interface的接口方法 public interface ItemService { void sayHello(); } public class ItemServiceImpl implements ItemService { @Override public void sayHello() { System.out.println("hello world~"); } } //代理类 public class MyInvocationHandler implements InvocationHandler { private Object realObject; public void setRealObject(Object realObject) { this.realObject = realObject; } public Object getRealObject() { return realObject; } public MyInvocationHandler(Object realObject){ super(); this.realObject=realObject; } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("before running ~~"); Object ret=method.invoke(realObject,args); System.out.println("after running ~~~"); return ret; } } // 实现类 public class main { public static void main(String[] args) { ItemService itemService=new ItemServiceImpl(); MyInvocationHandler handler=new MyInvocationHandler(itemService); ItemService proxy= (ItemService) Proxy.newProxyInstance(itemService.getClass().getClassLoader(),itemService.getClass().getInterfaces(),handler); proxy.sayHello(); } }
2.CGLIB implementation
CGLIB (Code Generation Library) is an ASM-based bytecode generation library that allows us to modify and dynamically generate bytecode at runtime. CGLIB implements proxy through inheritance;
Enhancer: to specify the target object to be proxied and the object that actually handles the proxy logic. Finally, the proxy object is obtained by calling the create() method. All non-final method calls on this object will be forwarded to MethodInterceptor;
MethodInterceptor: Method calls of dynamic proxy objects will be forwarded to the intercept method for enhancement;
//1.需要引入//2.方法 public class ItemServiceImpl { void sayHello(String name) { System.out.println("hello world~"+name); } } //3.cglib代理实现类 public class MyInterceptor implements MethodInterceptor { private Object realObject; public void setRealObject(Object realObject) { this.realObject = realObject; } public Object getRealObject() { return realObject; } public MyInterceptor(Object realObject){ this.realObject=realObject; } @Override public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable { System.out.println("before running ~~"); System.out.println(method); System.out.println(Arrays.toString(objects)); Object ret=methodProxy.invoke(realObject,objects); System.out.println("after running ~~"); return ret; } } //4.实现类 public class main { public static void main(String[] args) { ItemServiceImpl itemService=new ItemServiceImpl(); Enhancer enhancer=new Enhancer(); enhancer.setSuperclass(ItemServiceImpl.class); enhancer.setCallback(new MyInterceptor(itemService)); ItemServiceImpl imp= (ItemServiceImpl) enhancer.create(); imp.sayHello("张三"); } } cglib cglib 3.2.6
Summary:
JDK native dynamic proxy is natively supported by Java and does not require any external dependencies, but It can only proxy based on interfaces;
CGLIB proxy through inheritance, whether the target object implements the interface or not, it can proxy, but it cannot handle the final situation
The above is the detailed content of Introduction to the implementation methods of JAVA dynamic proxy and CGLIB mode (code example). For more information, please follow other related articles on the PHP Chinese website!