------- android培训 、 java培训 、期待与您交流! ----------
proxy(代理)
概念
代理就是实现AOP功能的核心与关键技术
动态代理类:JVM可以再运行期动态生成类的字节码,这种动态生成的类往往被用于代理类,即动态代理
JVM生成的动态代理类必须实现一个或多个接口,所以JVM生成的类只能用于具有相同接口的目标类的代理
实现动态代理常用的两个静态方法
ProxyClass(Loader,interface.class);
getProxyClass()
newProxyInstance(Loader,interface.class,InvocationHandler);
由Proxy类的静态方法创建的动态代理类具有以下特点:
定义接口:
package cn.hmm.Proxy;
import java.lang.reflect.Method;
public interface Advice {
void beforeMethod(Method method);
void afterMehod(Method method);
}
接口实现:
package cn.hmm.Proxy;
import java.lang.reflect.Method;
public class MyAdvice implements Advice {
long beginTime = 0;
long endTime = 0;
public void beforeMethod(Method method) {
// TODO 自动生成的方法存根
System.out.println("到黑马来学习了.");
beginTime = System.currentTimeMillis();
}
public void afterMehod(Method method) {
// TODO 自动生成的方法存根
System.out.println("从黑马毕业了.");
endTime = System.currentTimeMillis();
System.out.println(method.getName()+" running:"+(endTime-beginTime));
}
}
代理测试:
package cn.hmm.Proxy;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.Collection;
public class ProxyDemo {
public static void main(String[] args) throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
// TODO 自动生成的方法存根
Class clazzProxy1 = Proxy.getProxyClass(Collection.class.getClassLoader(), Collection.class);
System.out.println(clazzProxy1.getName());
Constructor[] constructors = clazzProxy1.getConstructors();
System.out.println("--------------begin constructors list------------");
//列举构造方法
for(Constructor constructor:constructors){
String name = constructor.getName();
StringBuilder sBuilder = new StringBuilder(name);
sBuilder.append('(');
Class[] clazzParams = constructor.getParameterTypes();
for(Class clazzParam : clazzParams){
sBuilder.append(clazzParam.getName()).append(',');
}
sBuilder.deleteCharAt(sBuilder.length()-1);
if(clazzParams != null && clazzParams.length !=0)
sBuilder.append(')');
System.out.println(sBuilder);
//System.out.println(constructor.getName());
}
//列举方法
Method[] methods = clazzProxy1.getMethods();
System.out.println("--------------begin Mehods list------------");
for(Method method:methods){
String name1 = method.getName();
StringBuilder sBuilder1 = new StringBuilder(name1);
sBuilder1.append('(');
Class[] clazzParams1 = method.getParameterTypes();
for(Class clazzParam : clazzParams1){
sBuilder1.append(clazzParam.getName()).append(',');
}
if(clazzParams1 != null && clazzParams1.length !=0)
sBuilder1.deleteCharAt(sBuilder1.length()-1);
sBuilder1.append(')');
System.out.println(sBuilder1);
}
System.out.println("--------------begin Create instance object------------");
Constructor constructor = clazzProxy1.getConstructor(InvocationHandler.class);
/* class MyInvocationHandler1 implements InvocationHandler{
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
// TODO 自动生成的方法存根
return null;
}
}*/
//Collection proxy1 = (Collection)constructor.newInstance(new MyInvocationHandler1());
Collection proxy1 = (Collection)constructor.newInstance(new InvocationHandler(){
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
// TODO 自动生成的方法存根
return null;
}
});
System.out.println(proxy1);
/*Collection proxy3 = (Collection)Proxy.newProxyInstance(
Collection.class.getClassLoader(),
new Class[]{Collection.class},
new InvocationHandler() {
ArrayList target = new ArrayList();
@Override
//哪个调用了代理,代理的方法,方法的参数
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
// TODO 自动生成的方法存根
System.out.println(proxy.getClass().getName());
long beginTime = System.currentTimeMillis();
Object retVal = method.invoke(target, args);
long endTime = System.currentTimeMillis();
System.out.println(method.getName()+" running:"+(endTime-beginTime));
return retVal;
}
}
);
proxy3.add("zxx");
proxy3.add("lhm");
proxy3.add("bxd");
System.out.println(proxy3.size());*/
final ArrayList target = new ArrayList();
Collection proxy3 = (Collection)getProxy(target,new MyAdvice());
proxy3.add("zxx");
proxy3.add("lhm");
proxy3.add("bxd");
System.out.println(proxy3.size());
}
private static Object getProxy(final Object target,final Advice advice) {
//Collection proxy3 = (Collection)Proxy.newProxyInstance(
//Collection.class.getClassLoader(),
//new Class[]{Collection.class},
Object proxy3 = (Object)Proxy.newProxyInstance(
target.getClass().getClassLoader(),
target.getClass().getInterfaces(),
new InvocationHandler() {
@Override
//哪个调用了代理,代理的方法,方法的参数
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
// TODO 自动生成的方法存根
/*System.out.println(proxy.getClass().getName());
long beginTime = System.currentTimeMillis();
Object retVal = method.invoke(target, args);
long endTime = System.currentTimeMillis();
System.out.println(method.getName()+" running:"+(endTime-beginTime));
return retVal;*/
System.out.println(proxy.getClass().getName());
advice.beforeMethod(method);
Object retVal = method.invoke(target, args);
advice.afterMehod(method);
return retVal;
}
}
);
return proxy3;
}
}
------- Windows Phone 7手机开发、.Net培训、期待与您交流! -------
详细请查看:www.itheima.com