java 动态代理实现
2015-09-09 18:15
567 查看
先附上项目结构:
步骤:
1.创建IFly接口:
2.创建Bird类,并让它实现IFly:
3.创建MyProxy类,并让它实现InvocationHandler
5.测试结果:
before invoke
A bird is flying
after invoke
步骤:
1.创建IFly接口:
package glut.proxy; public interface IFly { void fly(); }
2.创建Bird类,并让它实现IFly:
package glut.proxy; public class Bird implements IFly { public void fly() { System.out.println("A bird is flying"); } }
3.创建MyProxy类,并让它实现InvocationHandler
package glut.proxy; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.UndeclaredThrowableException; public class MyProxy implements InvocationHandler { // 目标实例 private Object targetInstance; // 不允许无参构造函数 private MyProxy() { // TODO Auto-generated constructor stub } public MyProxy(Object targetInstance) { this.targetInstance = targetInstance; } /** * Processes a method invocation on a proxy instance and returns the result. * This method will be invoked on an invocation handler when a method is * invoked on a proxy instance that it is associated with. * 每次调用目标实例的方法时,会回调invoke方法 * @param 代理对象实例 * * @param 目标实例被调用的方法名 * * @param 目标实例被调用的方法的参数 * * @return 目标实例调用的方法的返回值 * * * @throws Throwable * the exception to throw from the method invocation on the * proxy instance. The exception's type must be assignable * either to any of the exception types declared in the * {@code throws} clause of the interface method or to the * unchecked exception types {@code java.lang.RuntimeException} * or {@code java.lang.Error}. If a checked exception is thrown * by this method that is not assignable to any of the exception * types declared in the {@code throws} clause of the interface * method, then an {@link UndeclaredThrowableException} * containing the exception that was thrown by this method will * be thrown by the method invocation on the proxy instance. * * @see UndeclaredThrowableException */ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { // TODO Auto-generated method stub beforeInvoke(); Object invoke = method.invoke(targetInstance, args); afterInvoke(); return invoke; } /** * 调用之前 */ private void afterInvoke() { // TODO Auto-generated method stub System.out.println("after invoke"); } /** * 调用之后 */ private void beforeInvoke() { // TODO Auto-generated method stub System.out.println("before invoke"); } }4.测试代码:
package glut.test; import glut.proxy.Bird; import glut.proxy.IFly; import glut.proxy.MyProxy; import java.lang.reflect.Proxy; import org.junit.Test; public class MyTest { @Test public void test() { IFly bird = (IFly) Proxy.newProxyInstance(IFly.class.getClassLoader(), new Class[] { IFly.class }, new MyProxy(new Bird())); bird.fly(); } }
5.测试结果:
before invoke
A bird is flying
after invoke
相关文章推荐
- java-基础巩固 3
- java String的几个函数
- java如何实现发邮件功能。
- 基于java过滤器实现的ajax跨域解决方案
- Java的强 软 弱 虚引用
- 第一个java程序
- Java:类与继承
- Web开发:Struts2 Spring Hibernate整合(三)下——Hibernate的使用
- 解密java实现Excel导入导出
- java并发与线程池应用
- 详解struts.properties
- java获取磁盘空间大小
- 【Java笔记】——将晦涩难懂的IO流形象化
- java生成报表
- Java 7 NIO.2学习(Ing)
- maven版本问题
- 黑马训练营java学习笔记:输入/输出
- 关于thrift java实例入门
- Java实现常见排序
- Web开发:Struts2 Spring Hibernate整合(三)上——Hibernate的使用