1. 程式人生 > >Java-動態代理

Java-動態代理

tid some case interface 創建 lan class row art

JDK

 1 import java.lang.reflect.InvocationHandler;
 2 import java.lang.reflect.Method;
 3 
 4 public class MyInvocationHandler implements InvocationHandler {
 5 
 6     private Object target;//目標對象
 7     public MyInvocationHandler(Object target) {
 8         this.target = target;
 9     }
10     public
MyInvocationHandler() {} 11 12 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { 13 System.out.println("invoke方法"); 14 //獲取目標方法 15 Object result = method.invoke(target, args); 16 17 if(result != null) { 18 String str = (String)result;
19 result = str.toUpperCase();//修改目標方法返回值結果 20 } 21 return result; 22 } 23 }

1 public interface SomeService { 2 String doSmoe(); 3 }

1 public class SomeServiceImpl implements SomeService {
2 
3     public String doSmoe() {
4         System.out.println("目標方法");
5         return
"ok"; 6 } 7 }
 1 import java.lang.reflect.Proxy;
 2 
 3 public class Test {
 4 
 5     public static void main(String[] args) {
 6         
 7         SomeService target = new SomeServiceImpl();
 8         
 9         //動態代理對象
10         MyInvocationHandler h = new MyInvocationHandler(target);
11         
12         SomeService proxy = (SomeService)Proxy.newProxyInstance(target.getClass().getClassLoader(), 
13                 target.getClass().getInterfaces(), h);
14         
15         String doSmoe = proxy.doSmoe();
16         System.out.println(doSmoe);
17     }
18 }

CGLIB

1         <!-- cglib動態代理 -->
2         <dependency>
3             <groupId>cglib</groupId>
4             <artifactId>cglib</artifactId>
5             <version>3.2.6</version>
6         </dependency>

 1 import java.lang.reflect.Method;
 2 
 3 import net.sf.cglib.proxy.MethodInterceptor;
 4 import net.sf.cglib.proxy.MethodProxy;
 5 
 6 public class MyInterceptor implements MethodInterceptor {
 7     
 8     private Object target;
 9     public MyInterceptor() {
10         super();
11     }
12     public MyInterceptor(Object target) {
13         super();
14         this.target = target;
15     }
16 
17     public Object intercept(Object arg0, Method method, Object[] args, MethodProxy proxy) throws Throwable {
18         System.out.println("執行目標方法之前");
19         Object result = method.invoke(target, args);
20         System.out.println("執行目標方法之後");
21         if(result != null) {
22             result = "hello" + result;
23         }
24         return result;
25     }
26 }
 1 import net.sf.cglib.proxy.Enhancer;
 2 
 3 public class ProxyFactory {
 4 
 5     public Object createProxy(Object target) {
 6         //定義增強器對象
 7         Enhancer en  = new Enhancer();
 8         //指定父類
 9         en.setSuperclass(target.getClass());
10         //指定調用處理器對象
11         MyInterceptor h = new MyInterceptor(target);
12         en.setCallback(h);
13         return en.create();//創建cglib對象
14     }
15 }
1 public class TargetClass {
2 
3     public String doSome() {
4         System.out.println("目標類中的目標方法");
5         return " world";
6     }
7 }
 1 public class Test {
 2     public static void main(String[] args) {
 3         TargetClass target = new TargetClass();
 4         
 5         ProxyFactory factory = new ProxyFactory();
 6         TargetClass proxy = (TargetClass)factory.createProxy(target);
 7         
 8         System.out.println("proxy:" + proxy.getClass().getName());
 9         
10         String doSome = proxy.doSome();
11         System.out.println(doSome);
12     }
13 }

Java-動態代理