码迷,mamicode.com
首页 > 编程语言 > 详细

Java-动态代理

时间:2018-10-09 13:30:22      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:动态   intercept   java   父类   reflect   ice   voc   创建   lib   

 

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-动态代理

标签:动态   intercept   java   父类   reflect   ice   voc   创建   lib   

原文地址:https://www.cnblogs.com/wang1001/p/9759685.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!