Jfinal啓動原理及源碼簡析

如下全部源碼只截取了部分代碼,標題即爲類名app

一、Web.xml

<filter-name>jfinal</filter-name>
<filter-class>com.jfinal.core.JFinalFilter</filter-class>

二、JFinalFilter

if (jfinal.init(jfinalConfig, filterConfig.getServletContext()) == false)

三、Jfinal

boolean init(JFinalConfig jfinalConfig, ServletContext servletContext) {

   Config.configJFinal(jfinalConfig); // start plugin and init log factory in this method

}

四、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配置文件this

五、jfinalConfig

public void configRoute(Routes me) {

   //增長自定義route

   me.add(new ApiRoute());

六、  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;

}

七、JFinalFilter

加載完jfinalConfig回到JFinalFilterurl

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最核心的方法xml

這裏的handlre來自JFinalFilter.init方法中52行handler=jfinal.getHandler();blog

八、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的事情。看下ActionHandlerget

九、ActionHandler

  1)、根據ActionMapping得到相應的Action,源碼

Action action = actionMapping.getAction(target, urlPara);

   2)、而後利用反射進行方法的調用,最後把結果映射到相應的頁面上去servlet

new Invocation(action, controller).invoke();
相關文章
相關標籤/搜索