您好,登錄后才能下訂單哦!
靜態代理
第一種實現(基于接口):
1》接口
public interface Hello { void say(String msg); }
2》目標類,至少實現一個接口
public class HelloImpl implements Hello { public void say(String msg) { System.out.println("Hi,"+msg); } }
3》代理類(與目標類實現相同接口,從而保證功能一致)
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》測試
/** * @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"); } }
第二種實現(基于目標類):
1>目標類
public class HelloTarget { public void sayHello(String name){ System.out.println("Hi,"+name); } }
2>代理類(通過繼承目標類,保證功能一致)
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>測試
public class Main { public static void main(String[] args) throws Exception { HelloTarget target = new HelloTarget(); HelloProxy proxy= new HelloProxy(target); proxy.sayHello("LZHL"); } }
動態代理
動態代理的代理類是在程序運行期間動態生成的,也有兩種實現,一種是JDK動態代理,一種是CGLib動態代理
1》JDK動態代理(基于接口實現,與目標類實現相同接口,從而保證功能一致)
/** * @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: 代理對象 * method: 目標對象的方法對象 * args: 目標對象方法的參數 * return: 目標對象方法的返回值 */ 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"); //可以把InvocationHandler提取出來,單獨寫一個類,為了方便大家看,這里我用內部類的形式 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"); } }
2》CGLib動態代理(基于目標類,通過繼承目標類,從而保證功能一致),需要導入cglib-3.2.4.jar包
pom.xml
<dependencies> <!-- https://mvnrepository.com/artifact/cglib/cglib --> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>3.2.4</version> </dependency> </dependencies>
1)目標類
public class Hi { public void sayHi(String msg){ System.out.println("Hi,"+msg); } }
2)測試
/** * @Author LZHL * @Create 2017-02-19 13:19 * @Description */ public class Main { public static void main(String[] args) { Enhancer enhancer = new Enhancer(); //設置父類 enhancer.setSuperclass(Hi.class); //設置回調函數 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"); //可以把MethodInterceptor提取出來,單獨寫一個類,為了方便大家看,這里我用內部類的形式 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"); } }
以上所述是小編給大家介紹的Java靜態代理和動態代理總結,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復大家的!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。