62,635
社区成员




1 package com.yixi.proxy;
2
3 public interface UserService {
4
5 public int save() ;
6
7 public void update(int id);
8
9 }
复制代码1 package com.yixi.proxy;
2
3 public class UserServiceImpl implements UserService {
4
5 @Override
6 public int save() {
7 System.out.println("user save....");
8 return 1;
9 }
10
11 @Override
12 public void update(int id) {
13 System.out.println("update a user " + id);
14 }
15
16 }
1 package com.yixi.proxy;
2
3 import java.lang.reflect.InvocationHandler;
4 import java.lang.reflect.Method;
5
6 public class TimeInvocationHandler implements InvocationHandler {
7
8 @Override
9 public Object invoke(Object proxy, Method method, Object[] args)
10 throws Throwable {
11 System.out.println("startTime : " +System.currentTimeMillis());
12 Object obj = method.invoke(proxy, args);
13 System.out.println("endTime : " +System.currentTimeMillis());
14 return obj;
15 }
16
17 }
1 package com.yixi.proxy;
2 import java.lang.reflect.Proxy;
3
4 public class Test {
5
6 public static void main(String[] args) {
7 TimeInvocationHandler timeHandler = new TimeInvocationHandler();
8 UserService u = (UserService) Proxy.newProxyInstance(UserServiceImpl.class.getClassLoader(), UserServiceImpl.class.getInterfaces(), timeHandler);
9 u.update(2);
10 u.save();
11 }
12 }
复制代码 1 startTime : 1352877835040
2 startTime : 1352877835040
3 startTime : 1352877835040
4 Exception in thread "main" java.lang.reflect.UndeclaredThrowableException
5 at $Proxy0.update(Unknown Source)
6 at com.yixi.proxy.Test.main(Test.java:11)
7 Caused by: java.lang.reflect.InvocationTargetException
8 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
9 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
10 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
11 at java.lang.reflect.Method.invoke(Method.java:597)
12 at com.yixi.proxy.TimeInvocationHandler.invoke(TimeInvocationHandler.java:12)
13 ... 2 more
1 public Object invoke(Object proxy, Method method, Object[] args)
2 throws Throwable {
3 System.out.println("startTime : " +System.currentTimeMillis());
4 Object obj = method.invoke(proxy, args);
5 System.out.println("endTime : " +System.currentTimeMillis());
6 return obj;
7 }
1 package com.yixi.proxy;
2
3 import java.lang.reflect.InvocationHandler;
4 import java.lang.reflect.Method;
5
6 public class TimeInvocationHandler implements InvocationHandler {
7
8 private Object o;
9
10 public TimeInvocationHandler(Object o){
11 this.o = o;
12 }
13
14 @Override
15 public Object invoke(Object proxy, Method method, Object[] args)
16 throws Throwable {
17 System.out.println("startTime : " +System.currentTimeMillis());
18 Object obj = method.invoke(o, args);
19 System.out.println("endTime : " +System.currentTimeMillis());
20 return obj;
21 }
22
23 }
1 package com.yixi.proxy;
2
3 import java.lang.reflect.Proxy;
4
5 public class Test {
6
7 public static void main(String[] args) {
8 TimeInvocationHandler timeHandler = new TimeInvocationHandler(new UserServiceImpl());
9 UserService u = (UserService) Proxy.newProxyInstance(UserServiceImpl.class.getClassLoader(), UserServiceImpl.class.getInterfaces(), timeHandler);
10 u.update(2);
11 u.save();
12 }
13 }
1 package com.yixi.proxy;
2
3 import java.lang.reflect.InvocationHandler;
4 import java.lang.reflect.Method;
5 import java.lang.reflect.Proxy;
6
7 public class Test {
8
9 public static void main(String[] args) {
10 final UserServiceImpl usi = new UserServiceImpl();
11 UserService u = (UserService) Proxy.newProxyInstance(
12 usi.getClass().getClassLoader(),
13 usi.getClass().getInterfaces(),
14 new InvocationHandler() {
15
16 @Override
17 public Object invoke(Object proxy, Method method, Object[] args)
18 throws Throwable {
19 System.out.println("startTime : " +System.currentTimeMillis());
20 Object obj = method.invoke(usi, args);
21 System.out.println("endTime : " +System.currentTimeMillis());
22 return obj;
23 }
24 });
25 u.update(2);
26 u.save();
27 }
28 }
1 @Override
2 public Object invoke(Object proxy, Method method, Object[] args)
3 throws Throwable {
4 System.out.println("startTime : " +System.currentTimeMillis());
5 Object obj = method.invoke(usi, args);
6 System.out.println("endTime : " +System.currentTimeMillis());
7 return obj;
8 }
复制代码 1 public class Test {
2
3 public static void main(String[] args) {
4 final UserServiceImpl usi = new UserServiceImpl();
5 UserService u = (UserService) Proxy.newProxyInstance(
6 usi.getClass().getClassLoader(),
7 usi.getClass().getInterfaces(),
8 new InvocationHandler() {
9
10 @Override
11 public Object invoke(Object proxy, Method method, Object[] args)
12 throws Throwable {
13 return null;
14 }
15 });
16 u.update(2);
17 u.save();
18 }
19 }
1 public interface UserService {
2
3 public int save() ;
4
5 public void update(int id);
6
7 }