【Jfinal源碼】第一章 com.jfinal.core.JFinalFilter(1)

前言:
首先在gitosc獲取到jfinal的源碼,本學習筆記使用的是jfinal2.2版本。git


從web.xml開始,咱們去學習jfinal是怎麼從路由請求,到業務處理,最後的返回結果web

源碼下有示例的web.xml,整個框架的入口是JFinalFilter
<!--整個框架的入口-->
<filter>
	<filter-name>jfinal</filter-name>
		<filter-class>com.jfinal.core.JFinalFilter</filter-class>
	<init-param>
	<param-name>configClass</param-name>
		<param-value>common.JFinalDemoConfig</param-value>
	</init-param>
</filter>
	
<filter-mapping>
	<filter-name>jfinal</filter-name>
	<url-pattern>/*</url-pattern>
</filter-mapping>

JFinalFilter 的init方法

boolean init(JFinalConfig jfinalConfig, ServletContext servletContext) {
	this.servletContext = servletContext;
	this.contextPath = servletContext.getContextPath();
	
	// 初始化webRootpath	
	initPathUtil();
	
	// 啓動插件並初始化日誌工廠(本章主要講該部分)
	Config.configJFinal(jfinalConfig);
	constants = Config.getConstants();
		
	initActionMapping();
	initHandler();
	initRender();
	initOreillyCos();
	initTokenManager();
		
	return true;
}

Config類

  1. initPathUtil 獲取webRootpath
  2. jfinalConfig.configConstant(constants);//調用實現類的配置信息初始化常量
* jfinalConfig實現類配置常量
*/
public void configConstant(Constants me) {
    // 加載少許必要配置,隨後可用getProperty(.)獲取值
    loadPropertyFile("a_little_config.txt");
}

/**
* loadPropertyFile會最終調用該方法
*/
public Properties loadPropertyFile(String fileName, String encoding) {
	prop = new Prop(fileName, encoding);
	return prop.getProperties();
}

/**
* @param fileName classpath目錄下的文件名     
* @param encoding 文件編碼,默認UTF-8
*/
public Prop(String fileName, String encoding) {
	InputStream inputStream = null;
	try {
		inputStream =Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);		// properties.load(Prop.class.getResourceAsStream(fileName));
		if (inputStream == null)
		    throw new IllegalArgumentException("Properties file not found in classpath: " + fileName);
		properties = new Properties();
		properties.load(new InputStreamReader(inputStream, encoding));
	} catch (IOException e) {
		throw new RuntimeException("Error loading properties file.", e);
	}finally {
		if (inputStream != null) 
		    try {
		        inputStream.close();
		    } catch (IOException e) {
		        LogKit.error(e.getMessage(), e);
		    }
	}
}

3.initLoggerFactory();就是初始化logger,運用了工廠模式,有JdkLogger和Loger4jLoggerapache

/**
 * 默認使用log4j做爲日誌實現
 */
static void init() {
	if (defaultLogFactory == null) {
		try {
			Class.forName("org.apache.log4j.Logger");
			Class<?> log4jLogFactoryClass =Class.forName("com.jfinal.log.Log4jLogFactory");
			defaultLogFactory = (ILogFactory)log4jLogFactoryClass.newInstance();	// return new Log4jLogFactory();
			} catch (Exception e) {
				defaultLogFactory = new JdkLogFactory();
		}
	}
}

4.jfinalConfig.configRoute(routes); 配置路由映射app

/**
 * 設置 controllerKey Controller viewPath 三者的映射關係
 * 注意裏面的這幾個異常,常常會遇到
 */
public Routes add(String controllerKey,	Class<? extends Controller> controllerClass, String viewPath) {
	if (controllerKey == null)
		throw new IllegalArgumentException(
				"The controllerKey can not be null");
	controllerKey = controllerKey.trim();
	if ("".equals(controllerKey))
		throw new IllegalArgumentException(
				"The controllerKey can not be blank");
	if (controllerClass == null)
		throw new IllegalArgumentException(
				"The controllerClass can not be null");
	if (!controllerKey.startsWith("/"))
		controllerKey = "/" + controllerKey;
	if (map.containsKey(controllerKey))
		throw new IllegalArgumentException(
				"The controllerKey already exists: " + controllerKey);

	map.put(controllerKey, controllerClass);

	if (viewPath == null || "".equals(viewPath.trim())) // view path is
		viewPath = controllerKey;

	viewPath = viewPath.trim();
	if (!viewPath.startsWith("/")) // "/" added to prefix
		viewPath = "/" + viewPath;

	if (!viewPath.endsWith("/")) // "/" added to postfix
		viewPath = viewPath + "/";

	// 獲取配置的基礎路徑(在Constant中進行賦值)
	if (baseViewPath != null) // support baseViewPath
		viewPath = baseViewPath + viewPath;
    
        // 獲得 controller view 的映射 
	viewPathMap.put(controllerKey, viewPath);
	return this;
}
相關文章
相關標籤/搜索