English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Static Proxy
First implementation (based on interface):
1Interface
public interface Hello { void say(String msg); }
2>Target class, at least implements one interface
public class HelloImpl implements Hello { public void say(String msg) { System.out.println("Hi,"+msg); } }
3>Proxy class (implements the same interface as the target class to ensure consistency of functionality)
public class HelloProxy implements Hello{ private Hello hello; public HelloProxy(Hello hello){ this.hello = hello; } public void say(String msg){ before(); hello.say(msg); after(); } private void before(){ System.out.println("Before"); } private void after(){ System.out.println("After"); } }
3>Test
/** * @Author LZHL * @Create 2017-02-19 10:26 * @Description */ public class Main { public static void main(String[] args) throws Exception { HelloImpl target = new HelloImpl(); HelloProxy proxy = new HelloProxy(target); proxy.say("LZHL"); } }
Second implementation (based on target class):
1>Target class
public class HelloTarget { public void sayHello(String name){ System.out.println("Hi,"+name); } }
2>Proxy class (inherits from the target class to ensure consistency of functionality)
public class HelloProxy extends HelloTarget{ private HelloTarget target; public HelloProxy(HelloTarget target){ this.target = target; } @Override public void sayHello(String name) { this.before(); target.sayHello(name); this.after(); } private void before(){ System.out.println("Before"); } private void after(){ System.out.println("After"); } }
3>Test
public class Main { public static void main(String[] args) throws Exception { HelloTarget target = new HelloTarget(); HelloProxy proxy = new HelloProxy(target); proxy.sayHello("LZHL"); } }
Dynamic Proxy
The proxy class of dynamic proxy is dynamically generated during program execution and has two implementations: JDK dynamic proxy and CGLib dynamic proxy
1JDK dynamic proxy (implemented based on interfaces, with the same interface as the target class to ensure consistency of functionality)
/** * @Author LZHL * @Create 2017-02-19 12:46 * @Description */ public class Main { public static void main(String[] args){ final HelloImpl target = new HelloImpl(); Object proxyInstance = Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), new InvocationHandler() { /* * proxy: The proxy object * method: The method object of the target object * args: The parameters of the target object's method * return: The return value of the target object's method */ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("before"); Object retValue = method.invoke(target, args); System.out.println("after"); return retValue; } }); Hello proxy = (Hello) proxyInstance; proxy.say("LYX"); //You can extract the InvocationHandler and write it as a separate class. For the convenience of everyone, here I use the form of an inner class class JDKProxy implements InvocationHandler { private Object target; public JDKProxy(Object target){ this.target = target; } public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { before(); Object result = method.invoke(target, args); after(); return result; } private void before(){ System.out.println("Before"); } private void after(){ System.out.println("After"); } } InvocationHandler ih = new JDKProxy(target); Object proxyInstance2 = Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), ih); Hello proxy2 = (Hello) proxyInstance2; proxy2.say("LZHL"); } }
2CGLib dynamic proxy (based on the target class, by inheriting the target class, thus ensuring consistency of functions) requires importing cglib-3.2.4.jar package
pom.xml
<dependencies> <!-- https://mvnrepository.com/artifact/cglib/cglib --> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>3.2.4</version> </dependency> </dependencies>
1) Target class
public class Hi { public void sayHi(String msg){ System.out.println("Hi,"+msg); } }
2) Test
/** * @Author LZHL * @Create 2017-02-19 13:19 * @Description */ public class Main { public static void main(String[] args) { Enhancer enhancer = new Enhancer(); //Set the superclass enhancer.setSuperclass(Hi.class); //Set the callback function enhancer.setCallback(new MethodInterceptor() { public Object intercept(Object target, Method method, Object[] args, MethodProxy methodProxy) throws Throwable { System.out.println("before"); Object retValue = methodProxy.invokeSuper(target, args); System.out.println("after"); return retValue; } }); Object proxy = enhancer.create(); Hi hi = (Hi) proxy; hi.sayHi("LXY"); //Can extract MethodInterceptor and write it as a separate class. For convenience, I use the form of an inner class here class CGLibProxy implements MethodInterceptor { public <T> T getProxy(Class<T> clazz){ return (T) Enhancer.create(clazz, this); } public Object intercept(Object target, Method method, Object[] args, MethodProxy proxy) throws Throwable { before(); Object result = proxy.invokeSuper(target, args); after(); return result; } private void before(){ System.out.println("Before"); } private void after(){ System.out.println("After"); } } CGLibProxy cgLibProxy = new CGLibProxy(); Hi hi2 = cgLibProxy.getProxy(Hi.class); hi2.sayHi("LZHL"); } }
The above-mentioned is the summary of Java static proxy and dynamic proxy introduced by the editor to everyone, hoping it will be helpful to everyone. If you have any questions, please leave a message, and the editor will reply to everyone in time!
Declaration: The content of this article is from the network, the copyright belongs to the original author, the content is contributed and uploaded by Internet users spontaneously, this website does not own the copyright, does not undergo manual editing, and does not assume relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report violations, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.)