Java动态代理和静态代理区别
静态代理
package staticproxy; /** * 接口 * @author newtouch * */ public interface IHello { public void sayHello(); public String doSomethong(String s); }
package staticproxy; /** * 实现类 * @author newtouch * */ public class Hello implements IHello{ @Override public void sayHello() { System.out.println("zhangxiongfeng"); } @Override public String doSomethong(String s) { return s; } }
package staticproxy; /** * 静态代理类 * @author newtouch * */ /** * * @author newtouch * * 问题 如果接口中有多个方法 静态代理中 就会有多重新代理的方法 * */ public class StaticProxy { private IHello iHello; public StaticProxy(IHello iHello) { this.iHello=iHello; } public void sayHello() { iHello.sayHello(); } public String doSomethong(String s) { return iHello.doSomethong(s); } }
package staticproxy; public class Test { public static void main(String[] args) { IHello iHello = new Hello(); StaticProxy staticProxy = new StaticProxy(iHello); staticProxy.sayHello(); System.out.println(staticProxy.doSomethong("zhangxiongfeng")); } }
动态代理:
package proxy; /** * 接口 * @author newtouch * */ public interface IHello { public void sayHello(); public String doSomethong(String s); }
package proxy; public class Hello implements IHello{ @Override public void sayHello() { System.out.println("zhangxiongfeng"); } @Override public String doSomethong(String s) { return s; } }
package proxy; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; public class DynamicProxy implements InvocationHandler { private IHello iHello; DynamicProxy(IHello iHello) { this.iHello = iHello; } /** * 调用接口 IHello接口中的 任何方法都是要用下面的方法执行 * * 可以不用像静态代理中 每个方法都必须重写 */ @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { // 在代理真实对象前我们可以添加一些自己的操作 System.out.println("before rent house"); System.out.println("Method:" + method); // 当代理对象调用真实对象的方法时,其会自动的跳转到代理对象关联的handler对象的invoke方法来进行调用 method.invoke(iHello, args); // 在代理真实对象后我们也可以添加一些自己的操作 System.out.println("after rent house"); return null; } }
package proxy; import java.lang.reflect.Proxy; public class Test { public static void main(String[] args) { // TODO Auto-generated method stub IHello iHello = new Hello(); DynamicProxy dynamicProxy = new DynamicProxy(iHello); IHello hello = (IHello) Proxy.newProxyInstance(dynamicProxy.getClass().getClassLoader(), iHello.getClass().getInterfaces(), dynamicProxy); // hello.sayHello(); System.out.println(hello.doSomethong("zhangxf")); } }
动态代理执行:
before rent house
Method:public abstract void proxy.IHello.sayHello()
zhangxiongfeng
after rent house
package proxy; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Proxy; public class ProxyFactory<T> { private T t; private InvocationHandler invocationHandler; public ProxyFactory(T t,InvocationHandler invocationHandler) { this.t = t; this.invocationHandler = invocationHandler; } public T newProxyInstance() { return (T)Proxy.newProxyInstance(invocationHandler.getClass().getClassLoader(), t.getClass().getInterfaces(), invocationHandler); } } 动态工厂