Jfinal启动原理及源码简析

以下所有源码只截取了部分代码,标题即为类名

1、Web.xml

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

2、JFinalFilter

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

3、Jfinal

1
2
3
4
5
boolean init(JFinalConfig jfinalConfig, ServletContext servletContext) {
 
   Config.configJFinal(jfinalConfig); // start plugin and init log factory in this method
 
}

4、Config

1
2
3
4
5
6
7
8
9
10
11
12
13
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

1
2
3
4
5
public void configRoute(Routes me) {
 
   //增加自定义route
 
   me.add(new ApiRoute());

6、  Routes

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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

1
2
3
4
5
6
7
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

1
2
3
4
5
6
7
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,

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

   2)、然后利用反射进行方法的调用,最后把结果映射到相应的页面上去

1
new Invocation(action, controller).invoke();

 

posted @   Genesisx  阅读(1302)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示