Java基礎入門,重學JAVA基礎(三):動態代理

 2023-11-07 阅读 32 评论 0

摘要:1.接口 public interface Hello {public void sayHello(); } ? Java基礎入門。2.實例類 public class Hello2 {public void sayHello() {System.out.println("hello world2!");} } public class Hello3 extends Hello2{} public class HelloImpl implements Hello{

1.接口

public interface Hello {public void sayHello();
}

?

Java基礎入門。2.實例類

public class Hello2 {public void sayHello() {System.out.println("hello world2!");}
}
public class Hello3 extends Hello2{}
public class HelloImpl implements Hello{@Overridepublic void sayHello() {System.out.println("hello world!");}
}

?

3.JDK動態代理

ublic class JdkTest implements InvocationHandler{private Object object;@SuppressWarnings("unchecked")public <T> T bind(Object obj){this.object = obj;return (T)Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), this);}@Overridepublic Object invoke(Object proxy, Method method, Object[] args)throws Throwable {System.out.println("before sayHello");method.invoke(object, null);System.out.println("after sayHello");return null;}public static void main(String[] args) {JdkTest test = new JdkTest();Hello hello = test.bind(new HelloImpl());hello.sayHello();}}

抓過程 重基礎、?

4.cglib動態代理

public class CglibTest implements MethodInterceptor{private Object obj;/*** 普通接口類代理* @author tomsnail* @date 2015年4月2日 上午10:36:10*/public <T> T instance(T obj){this.obj = obj;Enhancer enhancer = new Enhancer();enhancer.setSuperclass(this.obj.getClass());enhancer.setCallback(this);return (T)enhancer.create();}@Overridepublic Object intercept(Object arg0, Method arg1, Object[] arg2,MethodProxy arg3) throws Throwable {System.out.println("before sayHello");arg3.invoke(obj, arg2);System.out.println("after sayHello");return null;}/*** 無接口類代理* @author tomsnail* @date 2015年4月2日 上午10:35:58*/public <T> T instanceObject(T obj){T t = (T)Enhancer.create(obj.getClass(),new MethodInterceptor(){@Overridepublic Object intercept(Object arg0, Method arg1, Object[] arg2,MethodProxy arg3) throws Throwable {System.out.println("hello2 proxy");return arg3.invokeSuper(arg0, arg2);}});return t;}/*** 無接口類代理* @author tomsnail* @date 2015年4月2日 上午10:35:58*/public <T> T instanceSuperObject(T obj){T t = (T)Enhancer.create(obj.getClass(),NoOp.INSTANCE);//無任何代理時,調用父類方法實現return t;}public static void main(String[] args) {CglibTest test = new CglibTest();Hello hello = test.instance(new HelloImpl());hello.sayHello();Hello2 hello2 = test.instanceObject(new Hello2());//無接口類的動態代理
        hello2.sayHello();Hello2 hello3 = test.instanceSuperObject(new Hello3());//子類沒有重寫父類方法的動態代理
        hello3.sayHello();}}

5.小小總結一下

JAVA入門、JDK的動態代理只能通過接口進行處理,如果沒有接口的,會很難處理。cglib沒有這一限制。

還有就是性能,我看見一篇網上已經有了對比,在jdk7/8與cglib相比,反而jdk的性能很好,見http://www.cnblogs.com/haiq/p/4304615.html

?

重在基礎、轉載于:https://www.cnblogs.com/TomSnail/p/4386566.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/1/167957.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息