JFinal 初始化过程浅析

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JFinal 初始化过程浅析相关的知识,希望对你有一定的参考价值。

从 web 容器进行初始化。
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  3. <filter>
  4. <filter-name>jfinal</filter-name>
  5. <filter-class>com.jfinal.core.JFinalFilter</filter-class>
  6. <init-param>
  7. <param-name>configClass</param-name>
  8. <param-value>com.fw.config.MppConfig</param-value>
  9. </init-param>
  10. </filter>
  11. <filter-mapping>
  12. <filter-name>jfinal</filter-name>
  13. <url-pattern>/*</url-pattern>
  14. </filter-mapping>
  15. </web-app>
从 web.xml 可以看出,容器初始化的时候会加载 JFinalFilter 这个类(调用其 init 方法)。下面来看看 JFinalFilter 类做了些什么。
  1. public final class JFinalFilter implements Filter {
  2. private Handler handler;
  3. private String encoding;
  4. private JFinalConfig jfinalConfig;
  5. private Constants constants;
  6. private static final JFinal jfinal = JFinal.me();
  7. private static Log log;
  8. private int contextPathLength;
  9. public void init(FilterConfig filterConfig) throws ServletException {
  10. createJFinalConfig(filterConfig.getInitParameter("configClass"));
  11. if (jfinal.init(jfinalConfig, filterConfig.getServletContext()) == false)
  12. throw new RuntimeException("JFinal init error!");
  13. handler = jfinal.getHandler();
  14. constants = Config.getConstants();
  15. encoding = constants.getEncoding();
  16. jfinalConfig.afterJFinalStart();
  17. String contextPath = filterConfig.getServletContext().getContextPath();
  18. contextPathLength = (contextPath == null || "/".equals(contextPath) ? 0 : contextPath.length());
  19. }
  20. public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
  21. HttpServletRequest request = (HttpServletRequest)req;
  22. HttpServletResponse response = (HttpServletResponse)res;
  23. request.setCharacterEncoding(encoding);
  24. String target = request.getRequestURI();
  25. if (contextPathLength != 0)
  26. target = target.substring(contextPathLength);
  27. boolean[] isHandled = {false};
  28. try {
  29. handler.handle(target, request, response, isHandled);
  30. }
  31. catch (Exception e) {
  32. if (log.isErrorEnabled()) {
  33. String qs = request.getQueryString();
  34. log.error(qs == null ? target : target + "?" + qs, e);
  35. }
  36. }
  37. if (isHandled[0] == false)
  38. chain.doFilter(request, response);
  39. }
  40. public void destroy() {
  41. jfinalConfig.beforeJFinalStop();
  42. jfinal.stopPlugins();
  43. }
  44. private void createJFinalConfig(String configClass) {
  45. if (configClass == null)
  46. throw new RuntimeException("Please set configClass parameter of JFinalFilter in web.xml");
  47. Object temp = null;
  48. try {
  49. temp = Class.forName(configClass).newInstance();
  50. } catch (Exception e) {
  51. throw new RuntimeException("Can not create instance of class: " + configClass, e);
  52. }
  53. if (temp instanceof JFinalConfig)
  54. jfinalConfig = (JFinalConfig)temp;
  55. else
  56. throw new RuntimeException("Can not create instance of class: " + configClass + ". Please check the config in web.xml");
  57. }
  58. static void initLog() {
  59. log = Log.getLog(JFinalFilter.class);
  60. }
  61. }

1. createJFinalConfig 创建自定义配置类的一个实例

这个方法中的参数正是 web.xml 中的 JFinalFilter 的初始化参数,它继承于 JFinalConfig。
调用此方法会创建出一个 JFinalConfig 实例。

2. jfinal.init 加载配置并且初始化各个 JFinal 组件

代码如下:
  1. boolean init(JFinalConfig jfinalConfig, ServletContext servletContext) {
  2. this.servletContext = servletContext;
  3. this.contextPath = servletContext.getContextPath();
  4. initPathUtil();
  5. Config.configJFinal(jfinalConfig);
  6. constants = Config.getConstants();
  7. initActionMapping();
  8. initHandler();
  9. initRender();
  10. initOreillyCos();
  11. initTokenManager();
  12. return true;
  13. }
(1) 初始化项目根路径 contextPath
(2) Config.configJFinal 加载自定义配置(配置常量、添加 handler、添加 routes、添加插件、开启插件、添加 Interceptor),代码如下:
  1. static void configJFinal(JFinalConfig jfinalConfig) {
  2. jfinalConfig.configConstant(constants); initLogFactory();
  3. jfinalConfig.configRoute(routes);
  4. jfinalConfig.configPlugin(plugins); startPlugins(); // very important!!!
  5. jfinalConfig.configInterceptor(interceptors);
  6. jfinalConfig.configHandler(handlers);
  7. }
  1. public class MppConfig extends JFinalConfig {
  2. //配置常量
  3. public void configConstant(Constants me) {
  4. PropKit.use("jdbc.properties");
  5. me.setDevMode(PropKit.getBoolean("devMode", false));
  6. }
  7. //配置路由
  8. public void configRoute(Routes me) {
  9. me.add(new RoutesMapping());
  10. }
  11. public static C3p0Plugin createC3p0Plugin() {
  12. return new C3p0Plugin(PropKit.get("jdbcUrl"), PropKit.get("user"), PropKit.get("password").trim());
  13. }
  14. //配置插件
  15. public void configPlugin(Plugins me) {
  16. // 配置C3p0数据库连接池插件
  17. C3p0Plugin C3p0Plugin = createC3p0Plugin();
  18. me.add(C3p0Plugin);
  19. // 配置ActiveRecord插件
  20. ActiveRecordPlugin arp = new ActiveRecordPlugin(C3p0Plugin);
  21. me.add(arp);
  22. // 配置属性名(字段名)大小写不敏感容器工厂 oracle
  23. arp.setContainerFactory(new CaseInsensitiveContainerFactory());
  24. //缓存插件
  25. me.add(new EhCachePlugin());
  26. // 所有配置在 MappingKit 中搞定
  27. ModelMapping.mapping(arp);
  28. }
  29. //配置全局拦截器
  30. public void configInterceptor(Interceptors me) {
  31. me.add(new SessionInViewInterceptor());//session拦截器,用于在View模板中取出session值
  32. }
  33. //配置处理器,接管所有 web 请求
  34. public void configHandler(Handlers me) {
  35. me.add(new ContextPathHandler("contextPath"));//设置上下文路径
  36. }
  37. //系统启动完成后回调,如创建调度线程
  38. public void afterJFinalStart(){}
  39. //系统关闭前回调,如写回缓存
  40. public void beforeJFinalStop(){}
  41. }
(3) 初始化 ActionMapping、Handler、Render、等。




以上是关于JFinal 初始化过程浅析的主要内容,如果未能解决你的问题,请参考以下文章

初始JFinal

JFinal源码详解

浅析weex之vdom渲染

JFinal + B-JUI-前端选型过程

jfinal框架的初级学习

ThreadPoolExecutor任务提交过程源码浅析