JDK動態代理實現攔截器的邏輯

  1. 定義一個攔截器的接口
public interface Interceptor {
	public boolean before(Object proxy,Object target,Method method,Object[] args);
	public void around(Object proxy,Object target,Method method,Object[] args);
	public void after(Object proxy,Object target,Method method,Object[] args);
}

這裏定義了3個方法,分別有如下邏輯定義

  • before方法返回boolean值,它在真實對象前調用。當返回爲true時,則反射真實對象的方法;當返回爲false時,則調用around方法。
  • 在before方法返回爲false時,調用around方法
  • 在反射真實對象方法或者around方法執行後,調用after方法

實現這個Interceotor——MyInterceptor

public class MyInterceptor implements Interceptor{
	@Override
	public boolean before(Object proxy, Object target, Method method, Object[] args) {
		System.out.println("反射方法前的邏輯");
		return false;
	}
	@Override
	public void around(Object proxy, Object target, Method method, Object[] args) {
		System.out.println("取代了被代理對象的方法");
	}
	@Override
	public void after(Object proxy, Object target, Method method, Object[] args) {
		System.out.println("反射方法後的邏輯");
	}
}

它實現了所有的Interceptor接口方法,使用JDK動態代理,就可以去實現這些方法在是當時的調用邏輯了。

public class InterceptorJdkProxy implements InvocationHandler {

	private Object target;//真實對象
	private String interceptorClass = null;//攔截器全限定類名
	
	public InterceptorJdkProxy(Object target, String interceptorClass) {
		this.target = target;
		this.interceptorClass = interceptorClass;
	}
	
	public static Object bind(Object target,String interceptorClass) {
		return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), new InterceptorJdkProxy(target, interceptorClass));
	}

	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		if(interceptorClass == null) {
			return method.invoke(proxy, args);
		}else {
			Object result = null;
			Interceptor interceptor = (Interceptor) Class.forName(interceptorClass).newInstance();
			if(interceptor.before(proxy, target, method, args)) {
				result = method.invoke(target, args);
			}else {
				interceptor.around(proxy, target, method, args);
			}
			//調用後置方法
			interceptor.after(proxy, target, method, args);
			return result;
		}
	}
	
	public static void main(String[] args) {
		HelloWorld helloWorld = (HelloWorld) InterceptorJdkProxy.bind(new HelloWorldImpl(), "interceptor.MyInterceptor");
		helloWorld.sayHelloWorld();
	}
}

上面main方法中的HelloWorld,直接使用了另一篇文章中的對象實現JDK動態代理

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章