代理模式就是当客户端想要调用被代理类的方法时,不是直接调用被代理类的方法,而是通过被代理类的委托类来实现,也就是说客户端调用委托类,委托类再调用被代理类。这样做的好处是我们可以在委托类中在执行被代理类方法之前和之后进行相关的处理,而不去影响被代理类中的相关业务代码。
代理分为两种,静态代理和动态代理。动态代理有可分为JDK动态代理和CGLib动态代理。
public interface Subject {
void doSomething();
}
public class RealSubject implements Subject {
@Override
public void doSomething() {
System.out.println("我正在执行被代理类的方法。。。。");
}
}
public class Proxy implements Subject {
private Subject subject = null;
public Proxy(Subject subject){
this.subject = subject;
}
@Override
public void doSomething() {
before();
this.subject.doSomething();
after();
}
private void before(){
System.out.println("我是代理类,正在执行before()");
}
private void after(){
System.out.println("我是代理类,正在执行after()");
}
}
public class Client {
public static void main(String[] args) {
Proxy proxy = new Proxy(new RealSubject());
proxy.doSomething();
}
}
我是代理类,正在执行before()
我正在执行被代理类的方法。。。。
我是代理类,正在执行after()
public interface Subject {
String doSomething();
}
public class RealSubject implements Subject {
@Override
public String doSomething() {
System.out.println("我正在执行被代理类的方法。。。。");
return "success!";
}
}
public class MyInvocationHandler implements InvocationHandler {
private Object target = null;
public MyInvocationHandler(Object object){
this.target = object;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
before();
Object ret = method.invoke(this.target,args);
after();
return ret;
}
private void before(){
System.out.println("我是代理类,正在执行before()");
}
private void after(){
System.out.println("我是代理类,正在执行after()");
}
}
public class MyProxy {
private Object target = null;
private InvocationHandler handler = null;
public MyProxy(InvocationHandler handler,Object target){
this.handler = handler;
this.target = target;
}
Object getProxy(){
Object object = Proxy.newProxyInstance(
this.target.getClass().getClassLoader(),
this.target.getClass().getInterfaces(),
this.handler);
return object;
}
}
public class Client {
public static void main(String[] args) {
Subject subject = new RealSubject();
MyInvocationHandler handler = new MyInvocationHandler(subject);
Subject proxy = (Subject)new MyProxy(handler,subject).getProxy();
System.out.println("返回值 = " + proxy.doSomething()); ;
}
}
我是代理类,正在执行before()
我正在执行被代理类的方法。。。。
我是代理类,正在执行after()
返回值 = success!
public interface InvocationHandler {
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable;
}
public static Object newProxyInstance(ClassLoader loader,
Class<?>[] interfaces,
InvocationHandler h) {
//InvocationHandler是否为null ,是则抛出异常 NullPointerException
Objects.requireNonNull(h);
//由于没有设置安全管理器,返回的是null
final Class<?> caller = System.getSecurityManager() == null
? null
: Reflection.getCallerClass();
/*
* Look up or generate the designated proxy class and its constructor.
*/
//获取动态代理类的构造器
Constructor<?> cons = getProxyConstructor(caller, loader, interfaces);
return newProxyInstance(caller, cons, h);
}
//最终调用的是这个方法来获取代理类
private static Object newProxyInstance(Class<?> caller, // null if no SecurityManager
Constructor<?> cons,
InvocationHandler h) {
/*
* Invoke its constructor with the designated invocation handler.
*/
try {
if (caller != null) {
checkNewProxyPermission(caller, cons.getDeclaringClass());
}
return cons.newInstance(new Object[]{h});
} catch (IllegalAccessException | InstantiationException e) {
throw new InternalError(e.toString(), e);
} catch (InvocationTargetException e) {
Throwable t = e.getCause();
if (t instanceof RuntimeException) {
throw (RuntimeException) t;
} else {
throw new InternalError(t.toString(), t);
}
}
}
public class RealSubject {
public String doSomething() {
System.out.println("我正在执行被代理类的方法。。。。");
return "success!";
}
}
public class CglibProxy implements MethodInterceptor {
private Enhancer enhancer = new Enhancer();
public Object getProxy(Class clazz){
enhancer.setSuperclass(clazz);
enhancer.setCallback(this);
return enhancer.create();
}
@Override
public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
before();
Object result = methodProxy.invokeSuper(o,args);
after();
return result;
}
private void before(){
System.out.println("我是代理类,正在执行before()");
}
private void after(){
System.out.println("我是代理类,正在执行after()");
}
}
public class Client {
public static void main(String[] args) {
CglibProxy cglibProxy = new CglibProxy();
RealSubject proxy = (RealSubject)cglibProxy.getProxy(RealSubject.class);
System.out.println("返回值 = " + proxy.doSomething()); ;
}
}
我是代理类,正在执行before()
我正在执行被代理类的方法。。。。
我是代理类,正在执行after()
返回值 = success!