JDK动态代理实例
2010-12-03 00:07
387 查看
import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import org.Hello; public class SayHello implements Hello{ @Override public void hello() { // TODO Auto-generated method stub System.out.println("hello"); } static class MyInvocationHandler implements InvocationHandler{ private Object target; public MyInvocationHandler(Object target){ this.target=target; } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { // TODO Auto-generated method stub System.out.println(method.getName()+"-------start"); Object result=method.invoke(target, args); System.out.println(method.getName()+"--------end"); return result; } } static class ProxyFactory{ InvocationHandler h; Class cls; public ProxyFactory(String className,InvocationHandler h) throws ClassNotFoundException{ cls=Class.forName(className); this.h=h; } public Object create(){ return Proxy.newProxyInstance(cls.getClassLoader(), cls.getInterfaces(), h); } } public static void main(String[] args) throws ClassNotFoundException { Hello s=new SayHello(); InvocationHandler h=new MyInvocationHandler(s); ProxyFactory proxyFactory=new ProxyFactory(SayHello.class.getName(), h); Hello sf=(Hello) proxyFactory.create(); sf.hello(); } } -----------------会输出 hello-------start hello--------end
大家可以debug,看看代码怎么走的。
下次研究过后会给出更详细的说明,^_^ 要睡觉了
相关文章推荐
- cblib实现JDK动态代理的简单实例
- java 中动态代理(JDK,cglib)实例代码
- JDK和Cglib实现动态代理实例及优缺点分析
- 简单动态代理实例(使用jdk的proxy)
- JDK和Cglib实现动态代理实例及优缺点分析
- 使用jdk动态代理实例
- JDK和Cglib实现动态代理实例及优缺点分析
- spring学习笔记(6)AOP前夕[1]jdk动态代理实例解析
- java jdk动态代理 实例 简单明了 对初学者非常有效
- JDK动态代理实例
- Java代理(jdk静态代理、动态代理和cglib动态代理)实例及总结
- jdk动态代理小实例
- 一个简单 JDK 动态代理的实例
- jdk的动态代理---学习笔记
- Java JDK 动态代理(AOP)使用及实现原理分析
- java 反射和动态代理详解及实例代码
- 聊聊动态代理:jdk、cglib、javassist
- JDK 动态代理的简单理解
- 动态代理(jdk)+cglib技术
- 基于JDK动态代理和CGLIB动态代理的实现Spring注解管理事务(@Trasactional)到底有什么区别。