package com.dhcool.proxy;
import java.lang.reflect.Method;
/**
* 这个类是用于给动态代理类调用的类,实现这个类就可以让代理类为我们调用
* */
public interface Advise {
void beforeMethod(Method method);
void afterMethod(Method method);
}
package com.dhcool.proxy;
import java.lang.reflect.Method;
/**
* 实现自己的Advise然后实现相应的方法
* */
public class MyAdvise implements Advise{
@Override
public void beforeMethod(Method method) {
System.out.println("beforeMethod");
}
@Override
public void afterMethod(Method method) {
System.out.println("afterMethod");
}
}
package com.dhcool.proxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
/**
* 这个类继承Proxy动态代理,当我们创建一个MyProxy的时候要给构造方法传递一个我们自己的 调用处理器 InvocationHandler
* 这个类中还有可以设置目标类(实现接口的类),建议添加的内容Advise
*
* Proxy能够动态的获取一个实现接口的代理类,当我们调用Proxy的Proxy.newProxyInstance(loader, interfaces, h)方法的时候,我们要传递过去的有
* 1.一个类加载器(反射获得目标对象的类加载器:target.getClass().getClassLoader())
* 2.具体实现类的接口Class[]数组集合(我们可以通过反射获得,target.getClass.getInterfaces())
* 3.一个 调用控制器(InvocationHandler)这个类我们必须要自己实现,因为当我们获取了一个实例代理类之后,就可以去调用该类相应的方法,而这个调用过程就在
* 实例代理类中是这样实现的:比如我们的target类有一个方法叫add();在实例代理类中也会有一个方法叫做add();并且是Proxy帮我们已经实现好了的,方法的内容是这样的
* InvocationHandler实例对象去调用invoke(Object proxy, Method method, Object[] args),也就是我们初始化的invoke(Object proxy, Method method, Object[] args)
* 这个过程中会把Proxy实例代理类,调用具体的方法mehod,调用时的参数Object[] args传递过去。
*
*了解了以上的过程,我们需要做的就是去实现这个InvocationHandler,实现invoke方法,在invoke方法中就可以是实现我们的target的调用,也就是method.invoke(target,args);
*这样就一个过程就是动态代理过程。
* */
public class MyProxy {
private static final long serialVersionUID = 1L;
private InvocationHandler handler = null;
private Advise advise;
private Object target;
public MyProxy(){}
protected MyProxy(InvocationHandler h) {
this.handler = h;
}
//设置建议内容
public void setAdvise(Advise advise){
this.advise = advise;
}
//设置目标类
public void setTarget(Object target){
this.target = target;
}
//设置 调用处理器
public void setInvocationHander(InvocationHandler h ){
this.handler = h;
}
//获取动态代理类,返回的是一个对象Object类型,我们可以根据需要进行转换为相应的类
private Object getProxy(final Advise advise, final Object target) {
if(this.target == null){
return null;
}
if(this.handler == null){
if(this.advise == null){
return null;
}
return Proxy.newProxyInstance(this.target.getClass().getClassLoader(), this.target.getClass().getInterfaces(), new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
advise.beforeMethod(method);
Object returnValue = method.invoke(target, args);
advise.afterMethod(method);
return returnValue;
}
});
}else{
return Proxy.newProxyInstance(this.target.getClass().getClassLoader(), this.target.getClass().getInterfaces(), this.handler);
}
}
public Object getProxy(){
return this.getProxy(this.advise, this.target);
}
}
package com.dhcool.proxy;
import java.lang.annotation.Target;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.Collection;
public class ProxyTest {
public static void main(String[] args) {
//final Advise advise = new MyAdvise();
final Advise advise2 = new MyAdvise2();
//final Collection<String> target = new ArrayList<String>();
/*MyProxy myProxy1 = new MyProxy(new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
advise.beforeMethod(method);
advise.beforeMethod(method);
advise.afterMethod(method);
System.out.println("OK");
return method.invoke(target, args);
}
});
myProxy1.setTarget(target);
@SuppressWarnings("unchecked")
Collection<String> c = (Collection<String>)myProxy1.getProxy();
if(c == null){
System.out.println("数据不足不能获取代理对象");
return;
}
c.add("123");
System.out.println(c.size());*/
/*MyProxy myProxy = new MyProxy();
myProxy.setAdvise(advise);
myProxy.setTarget(target);
@SuppressWarnings("unchecked")
Collection<String> c2 = (Collection<String>)myProxy.getProxy();
if(c2 == null){
System.out.println("数据不足不能获取代理对象");
return;
}
c2.add("123");
System.out.println(c2.size()); */
//创建代理
MyProxy myProxy2 = new MyProxy();
//设置代理的附加建议代码
myProxy2.setAdvise(advise2);
//设置代理的实现目标接口
final MyInterfaces interfaces = new InterfaceImp();
myProxy2.setTarget(interfaces);
//获取动态代理类
MyInterfaces inter = (MyInterfaces)myProxy2.getProxy();
inter.add();
/*final MyInterfaces interfaces = new InterfaceImp();
//final Collection interfaces = new ArrayList();
Class[] clazz = interfaces.getClass().getInterfaces();
for (Class class1 : clazz) {
System.out.println(class1.getName());
}
MyInterfaces a = (MyInterfaces)Proxy.newProxyInstance(interfaces.getClass().getClassLoader(), interfaces.getClass().getInterfaces(), new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
advise2.afterMethod(method);
return method.invoke(interfaces, args);
}
});
System.out.println(a.delete());*/
}
}
package com.dhcool.proxy;
public interface MyInterfaces {
public void add();
public int delete();
public void update();
}
package com.dhcool.proxy;
public class InterfaceImp implements MyInterfaces {
@Override
public void add() {
System.out.println("增加");
}
@Override
public int delete() {
return 1;
}
@Override
public void update() {
System.out.println("更新");
}
}