JFinal 源码导读第三天(1) Interceptors

1.jfinalConfig.configInterceptor(interceptors);这段代码会执行我们的配置信息里面的

/**
	 * 配置全局拦截器
	 */
	public void configInterceptor(Interceptors me) {
		me.add(new BlogInterceptor1());
		me.add(new BlogInterceptor2());
	}
2.me.add的方法已经很简单啦,贴个代码应该一目了然
/**
 * The interceptors applied to all actions.
 */
final public class Interceptors {
	
	private final List<Interceptor> interceptorList = new ArrayList<Interceptor>();
	
	public Interceptors add(Interceptor globalInterceptor) {
		if (globalInterceptor != null)
			this.interceptorList.add(globalInterceptor);
		return this;
	}
	
	public Interceptor[] getInterceptorArray() {
		Interceptor[] result = interceptorList.toArray(new Interceptor[interceptorList.size()]);
		return result == null ? new Interceptor[0] : result;
	}
}
3.贴出BlogInterceptor1和 BlogInterceptor2
public class BlogInterceptor1 implements Interceptor {
	
	public void intercept(ActionInvocation ai) {
		System.out.println("Before invoking BlogInterceptor1" + ai.getActionKey());
		ai.invoke();
		System.out.println("After invoking BlogInterceptor1" + ai.getActionKey());
	}
}
public class BlogInterceptor2 implements Interceptor {
	
	public void intercept(ActionInvocation ai) {
		System.out.println("Before invoking BlogInterceptor2" + ai.getActionKey());
		ai.invoke();
		System.out.println("After invoking BlogInterceptor2" + ai.getActionKey());
	}
}
4.jfinalConfig.configHandler(handlers);基本上和上面初始化拦截器差不多
/**
 * 配置处理器
 */
public void configHandler(Handlers me) {
	me.add(new ContextPathHandler());
}

/**
 * Handlers.
 */
final public class Handlers {
	
	private final List<Handler> handlerList = new ArrayList<Handler>();
	
	public Handlers add(Handler handler) {
		if (handler != null)
			handlerList.add(handler);
		return this;
	}
	
	public List<Handler> getHandlerList() {
		return handlerList;
	}
}



你可能感兴趣的:(jFinal,interceptors)