1,創(chuàng)建一個UserService類:
public interface UserService { public String getTheName(int id); public Integer getTheAge(int id); }
2,創(chuàng)建實現(xiàn)類UserServiceImpl
public class UserServiceImpl implements UserService { @Override public String getTheName(int id) { return "小風"; } @Override public Integer getTheAge(int id) { return 10; } }
3, 測試類JDKProxyTest
public class JDKProxyTest implements InvocationHandler { private Object target; JDKProxyTest() { super(); } JDKProxyTest(Object target) { super(); this.target = target; } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("++++++調(diào)用方法之前: " + method.getName() + "++++++"); Object result = method.invoke(target, args); System.out.println("++++++調(diào)用方法之后: " + method.getName() + "++++++"+result); return result; } public static void main(String[] args) { UserServiceImpl userService = new UserServiceImpl(); InvocationHandler handler=new JDKProxyTest(userService); UserService userServiceProxy= (UserService) Proxy.newProxyInstance(userService.getClass().getClassLoader(), userService.getClass().getInterfaces(), handler); System.out.println(userServiceProxy.getTheName(1)); System.out.println(userServiceProxy.getTheAge(1)); } }
當前名稱:JDK動態(tài)代理demo-創(chuàng)新互聯(lián)
本文網(wǎng)址:http://jinyejixie.com/article30/dpdjpo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗、網(wǎng)頁設(shè)計公司、網(wǎng)站收錄、企業(yè)建站、網(wǎng)站改版、網(wǎng)站排名
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容