Java-动态代理

Posted 为你编程

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java-动态代理相关的知识,希望对你有一定的参考价值。

 

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-动态代理的主要内容,如果未能解决你的问题,请参考以下文章

Java设计模式-代理模式之动态代理(附源代码分析)

(java反射-JDK动态代理)+CGLIB动态代理

Java动态代理

Java动态代理

Java动态代理

动态代理