Jfinal启动原理及源码简析
以下所有源码只截取了部分代码,标题即为类名
1、Web.xml
<filter-name>jfinal</filter-name> <filter-class>com.jfinal.core.JFinalFilter</filter-class>
2、JFinalFilter
if (jfinal.init(jfinalConfig, filterConfig.getServletContext()) == false)
3、Jfinal
boolean init(JFinalConfig jfinalConfig, ServletContext servletContext) { Config.configJFinal(jfinalConfig); // start plugin and init log factory in this method }
4、Config
static void configJFinal(JFinalConfig jfinalConfig) { jfinalConfig.configConstant(constants); initLogFactory(); jfinalConfig.configRoute(routes); jfinalConfig.configPlugin(plugins); startPlugins(); // very important!!! jfinalConfig.configInterceptor(interceptors); jfinalConfig.configHandler(handlers); }
加载jfinalConfig配置文件
5、jfinalConfig
public void configRoute(Routes me) { //增加自定义route me.add(new ApiRoute());
6、 Routes
public Routes add(Routes routes) { if (routes != null) { routes.config(); // very important!!! for (Entry<String, Class<? extends Controller>> e : routes.map.entrySet()) { String controllerKey = e.getKey(); if (this.map.containsKey(controllerKey)) { throw new IllegalArgumentException("The controllerKey already exists: " + controllerKey); } this.map.put(controllerKey, e.getValue()); this.viewPathMap.put(controllerKey, routes.getViewPath(controllerKey)); } } return this; }
7、JFinalFilter
加载完jfinalConfig回到JFinalFilter
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { try { handler.handle(target, request, response, isHandled); }
// handler.handle(target, request, response, isHandled);是整个Filter最核心的方法
这里的handlre来自JFinalFilter.init方法中52行handler=jfinal.getHandler();
8、Jfinal
private void initHandler() { Handler actionHandler = new ActionHandler(actionMapping, constants); handler = HandlerFactory.getHandler(Config.getHandlers().getHandlerList(), actionHandler); }
handler是由HandlerFactory的getHandler方法得来的,此处使用handler子类ActionHandler,并且传进去了有两个参数,一个是ActionMapping类的变量,一个是constants。
ActionMapping在ActionMapping中定义了一个路由(routes)和一个Interceptors,这个routes类里面主要的核心是两个Map,主要处理处理一些关于ActionMapping中对应的ControllerKey与Controller.class的事情。看下ActionHandler
9、ActionHandler
1)、根据ActionMapping获得相应的Action,
Action action = actionMapping.getAction(target, urlPara);
2)、然后利用反射进行方法的调用,最后把结果映射到相应的页面上去
new Invocation(action, controller).invoke();