java的三种代理
/**
* 接口
*/
public interface Action {
/**
* dosomething
*/
void doSomething();
}
/**
* @author earthchen
* @date 2018/8/10
**/
public class RealObject implements Action {
@Override
public void doSomething() {
System.out.println(this.getClass().getName()+"dosomething....");
}
}
/**
* 代理类
*
* @author earthchen
* @date 2018/8/10
**/
public class Proxy implements Action {
private Action realObject;
public Proxy(Action realObject) {
this.realObject = realObject;
}
@Override
public void doSomething() {
System.out.println("proxy do");
realObject.doSomething();
}
}
需要维护一个接口的对象
public class Main {
public static void main(String[] args) {
Action action = new RealObject();
Proxy proxy = new Proxy(action);
proxy.doSomething();
}
}
这种代理方式需要代理对象和目标对象实现一样的接口。
/**
* @author earthchen
* @date 2018/8/10
**/
public interface Action {
void doSomeThing();
}
/**
* @author earthchen
* @date 2018/8/10
**/
public class RealObject1 implements Action {
@Override
public void doSomeThing() {
System.out.println(this.getClass().getName()+"do someThing...");
}
}
/**
* @author earthchen
* @date 2018/8/10
**/
public class RealObject2 implements Action {
@Override
public void doSomeThing() {
System.out.println(this.getClass().getName()+"do someThing...");
}
}
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
/**
* @author earthchen
* @date 2018/8/10
**/
public class DynamicProxyHandler implements InvocationHandler {
private Action testAction;
public DynamicProxyHandler(Action testAction) {
this.testAction = testAction;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//代理扩展逻辑
System.out.println("proxy do");
return method.invoke(testAction, args);
}
}
public class Main {
public static void main(String[] args) {
RealObject1 realObject1 = new RealObject1();
Action proxy1 = (Action) Proxy.newProxyInstance(ClassLoader.getSystemClassLoader(),
new Class[]{Action.class}, new DynamicProxyHandler(realObject1));
proxy1.doSomeThing();
RealObject2 realObject2 = new RealObject2();
Action proxy2 = (Action) Proxy.newProxyInstance(ClassLoader.getSystemClassLoader(),
new Class[]{Action.class}, new DynamicProxyHandler(realObject2));
proxy2.doSomeThing();
}
}
动态代理对象不需要实现接口,但是要求目标对象必须实现接口,否则不能使用动态代理。
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.2.5</version>
</dependency>
public class RealObject {
public void doSomeThing(){
System.out.println(this.getClass().getName()+"do someThing...");
}
}
import java.lang.reflect.Method;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
/**
* @author earthchen
* @date 2018/8/10
**/
public class CglibProxy implements MethodInterceptor {
private Object target;
public CglibProxy(Object object) {
this.target = object;
}
/**
* 为目标对象生成代理对象
*
* @return
*/
public Object getProxyInstance() {
//工具类
Enhancer en = new Enhancer();
//设置父类
en.setSuperclass(target.getClass());
//设置回调函数
en.setCallback(this);
//创建子类对象代理
return en.create();
}
@Override
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
System.out.println("before do someThing.....");
// 执行目标对象的方法
Object returnValue = method.invoke(target, objects);
System.out.println("after do someThing....");
return returnValue;
}
}