动态代理的步骤:
- 创建一个实现了InvocationHandler接口的类,必须重写接口里的invoke()方法。
- 创建被代理的类和接口
- 通过Proxy的静态方法 newProxyInsatance(ClassLoader loader,Class[] interfaces,InvocationHandler,handler)来创建一个代理
- 通过代理来调用方法
简单的动态代理实例
package com.sean.zzzjvm;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
/**
*
- @Author Sean
- @Date 2017/8/20 21:43.
-
@Version
*/
public class DynamicProxyTest {
<span class="hljs-comment" style="box-sizing: border-box; color: #999988; font-style: italic;">//定义一个接口</span>
<span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: #333333; font-weight: bold;">interface</span> <span class="hljs-title" style="box-sizing: border-box; color: #445588; font-weight: bold;">IHello</span></span>{
<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: #333333; font-weight: bold;">void</span> <span class="hljs-title" style="box-sizing: border-box; color: #990000; font-weight: bold;">sayHello</span><span class="hljs-params" style="box-sizing: border-box;">()</span></span>;
}
<span class="hljs-comment" style="box-sizing: border-box; color: #999988; font-style: italic;">//实现该接口的类</span>
<span class="hljs-keyword" style="box-sizing: border-box; color: #333333; font-weight: bold;">static</span> <span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: #333333; font-weight: bold;">class</span> <span class="hljs-title" style="box-sizing: border-box; color: #445588; font-weight: bold;">Hello</span> <span class="hljs-keyword" style="box-sizing: border-box; color: #333333; font-weight: bold;">implements</span> <span class="hljs-title" style="box-sizing: border-box; color: #445588; font-weight: bold;">IHello</span></span>{
<span class="hljs-annotation" style="box-sizing: border-box;">@Override</span>
<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: #333333; font-weight: bold;">public</span> <span class="hljs-keyword" style="box-sizing: border-box; color: #333333; font-weight: bold;">void</span> <span class="hljs-title" style="box-sizing: border-box; color: #990000; font-weight: bold;">sayHello</span><span class="hljs-params" style="box-sizing: border-box;">()</span> </span>{
System.out.println(<span class="hljs-string" style="box-sizing: border-box; color: #dd1144;">"hello world"</span>);
}
}
<span class="hljs-comment" style="box-sizing: border-box; color: #999988; font-style: italic;">//创建一个动态代理类,实现InvocationHandler接口</span>
<span class="hljs-keyword" style="box-sizing: border-box; color: #333333; font-weight: bold;">static</span> <span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: #333333; font-weight: bold;">class</span> <span class="hljs-title" style="box-sizing: border-box; color: #445588; font-weight: bold;">DynamicProxy</span> <span class="hljs-keyword" style="box-sizing: border-box; color: #333333; font-weight: bold;">implements</span> <span class="hljs-title" style="box-sizing: border-box; color: #445588; font-weight: bold;">InvocationHandler</span></span>{
Object originalObj;
<span class="hljs-comment" style="box-sizing: border-box; color: #999988; font-style: italic;">//创建一个代理的方法,在new DynamicProxy().bind(new Hello());执行</span>
<span class="hljs-function" style="box-sizing: border-box;">Object <span class="hljs-title" style="box-sizing: border-box; color: #990000; font-weight: bold;">bind</span> <span class="hljs-params" style="box-sizing: border-box;">(Object originalObj)</span></span>{
<span class="hljs-keyword" style="box-sizing: border-box; color: #333333; font-weight: bold;">this</span>.originalObj = originalObj;
<span class="hljs-comment" style="box-sizing: border-box; color: #999988; font-style: italic;">//返回一个代理对象</span>
<span class="hljs-keyword" style="box-sizing: border-box; color: #333333; font-weight: bold;">return</span> Proxy.newProxyInstance(originalObj.getClass().getClassLoader(),
originalObj.getClass().getInterfaces(),<span class="hljs-keyword" style="box-sizing: border-box; color: #333333; font-weight: bold;">this</span>);
}
<span class="hljs-comment" style="box-sizing: border-box; color: #999988; font-style: italic;">//默认重写的方法,在 hello.sayHello()执行</span>
<span class="hljs-annotation" style="box-sizing: border-box;">@Override</span>
<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: #333333; font-weight: bold;">public</span> Object <span class="hljs-title" style="box-sizing: border-box; color: #990000; font-weight: bold;">invoke</span><span class="hljs-params" style="box-sizing: border-box;">(Object proxy, Method method, Object[] args)</span> <span class="hljs-keyword" style="box-sizing: border-box; color: #333333; font-weight: bold;">throws</span> Throwable </span>{
System.out.println(<span class="hljs-string" style="box-sizing: border-box; color: #dd1144;">"welcome"</span>);
<span class="hljs-keyword" style="box-sizing: border-box; color: #333333; font-weight: bold;">return</span> method.invoke(originalObj,args);
}
}
<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box; color: #333333; font-weight: bold;">public</span> <span class="hljs-keyword" style="box-sizing: border-box; color: #333333; font-weight: bold;">static</span> <span class="hljs-keyword" style="box-sizing: border-box; color: #333333; font-weight: bold;">void</span> <span class="hljs-title" style="box-sizing: border-box; color: #990000; font-weight: bold;">main</span><span class="hljs-params" style="box-sizing: border-box;">(String[] args)</span></span>{
<span class="hljs-comment" style="box-sizing: border-box; color: #999988; font-style: italic;">// IHello hello = new Hello();</span>
<span class="hljs-comment" style="box-sizing: border-box; color: #999988; font-style: italic;">//调用动态代理的方法</span>
IHello hello = (IHello) <span class="hljs-keyword" style="box-sizing: border-box; color: #333333; font-weight: bold;">new</span> DynamicProxy().bind(<span class="hljs-keyword" style="box-sizing: border-box; color: #333333; font-weight: bold;">new</span> Hello());
hello.sayHello();
}
}
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于