Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the ways of Java dynamic proxy

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "what are the ways of Java dynamic agents". In daily operation, I believe that many people have doubts about the ways of Java dynamic agents. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "what are the ways of Java dynamic agents?" Next, please follow the editor to study!

There are two ways: 1, JDK dynamic proxy, using reflection mechanism to generate an anonymous class that implements the proxy interface, and calling InvokeHandler to deal with it before calling specific methods; 2. CGLIB dynamic proxy, using asm open source package, loads the class file of proxy object class and generates subclasses by modifying its byte code.

The operating environment of this tutorial: windows7 system, java8 version, DELL G3 computer.

Dynamic proxy is a very important application scenario of reflection. Dynamic agents are often used in some Java frameworks. For example, the AOP of Spring and the SPI interface of Dubbo are implemented based on Java dynamic proxy.

Dynamic proxies can be done in two ways:

JDK dynamic proxy: use the reflection mechanism to generate an anonymous class that implements the proxy interface, and call InvokeHandler to deal with it before calling the specific method.

CGLIB dynamic proxy: using ASM (open source Java bytecode editing library, operation bytecode) open source package, the class file of the proxy object class is loaded and processed by modifying its byte code to generate a subclass.

Difference: JDK proxy can only generate proxies for classes that implement the interface; CGlib implements proxies for classes, generates a subclass for the specified class and overrides the methods in it, which cannot proxy classes modified by final through the implementation of inheriting classes.

Force the use of CGlib

Specific code examples:

/ * Target interface class * / public interface UserManager {public void addUser (String id, String password); public void delUser (String id);} / * Interface implementation class * / public class UserManagerImpl implements UserManager {@ Override public void addUser (String id, String password) {System.out.println ("UserManagerImpl.addUser () method called!") ; @ Override public void delUser (String id) {System.out.println ("UserManagerImpl.delUser () method called!") ;}} / * JDK dynamic proxy class * / public class JDKProxy implements InvocationHandler {/ / the target object that needs to be proxied private Object targetObject; public Object newProxy (Object targetObject) {/ / pass the target object to proxy this.targetObject = targetObject / / returns the proxy object return Proxy.newProxyInstance (targetObject.getClass (). GetClassLoader (), targetObject.getClass (). GetInterfaces (), this);} / / invoke method @ Override public Object invoke (Object proxy, Method method, Object [] args) throws Throwable {/ / logical function checkPopedom (); Object ret = null / / call invoke method ret = method.invoke (targetObject, args); return ret;} private void checkPopedom () {/ / simulate check permission System.out.println ("check permission: checkPopedom ()!") }} / * CGlib dynamic proxy class * / public class CGLibProxy implements MethodInterceptor {/ / CGlib target object private Object targetObject; public Object createProxyObject (Object obj) {this.targetObject = obj; Enhancer enhancer = new Enhancer (); enhancer.setSuperclass (obj.getClass ()); enhancer.setCallback (this); Object proxyObj = enhancer.create () Return proxyObj;} @ Override public Object intercept (Object proxy, Method method, Object [] args, MethodProxy methodProxy) throws Throwable {Object obj = null; / / filter method if ("addUser" .equals (method.getName () {/ / check permission checkPopedom ();} obj = method.invoke (targetObject, args); return obj } private void checkPopedom () {System.out.println ("check permissions: checkPopedom ()!");}} / * * Test class * / public class ProxyTest {public static void main (String [] args) {UserManager userManager = (UserManager) new CGLibProxy (). CreateProxyObject (new UserManagerImpl ()); System.out.println ("CGLibProxy:"); userManager.addUser ("tom", "root") System.out.println ("JDKProxy:"); JDKProxy jdkProxy = new JDKProxy (); UserManager userManagerJDK = (UserManager) jdkProxy.newProxy (new UserManagerImpl ()); userManagerJDK.addUser ("tom", "root");}} / / run result CGLibProxy: check permission checkPopedom ()! The UserManagerImpl.addUser () method is called! JDKProxy: check permissions checkPopedom ()! The UserManagerImpl.addUser () method is dropped! At this point, the study of "what are the ways of Java dynamic agents" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report