1. 程式人生 > >原始碼有毒:Jfinal原始碼解析(二)

原始碼有毒:Jfinal原始碼解析(二)

public Routes add(String controllerKey, Class<? extends Controller> controllerClass, String viewPath) {
        if (controllerKey == null)
            throw new IllegalArgumentException("The controllerKey can not be null");
        // if (controllerKey.indexOf(".") != -1)
            // throw new IllegalArgumentException("The controllerKey can not contain dot character: \".\"");
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 controllerKey by default viewPath = controllerKey; viewPath = viewPath.trim(); if (!viewPath.startsWith("/")) // "/" added to prefix viewPath = "/" + viewPath; if (!viewPath.endsWith("/")) // "/" added to postfix viewPath = viewPath + "/"; if (baseViewPath != null) // support baseViewPath viewPath = baseViewPath + viewPath; viewPathMap.put(controllerKey, viewPath); return this; }

add方法中,把相應的Controller(value),和與之對於的路徑(key)儲存都了一個Map集合中
繼續看 jfinalConfig.configPlugin(plugins);這裡是專案配置各種外掛的地方,先看一個Demo

@Override
    public void configPlugin(Plugins pl) {
        DruidPlugin druidPlugin = new DruidPlugin(getProperty("jdbcUrl").trim(),
                getProperty("user").trim(), getProperty("password").trim(),
                getProperty("jdbcdriver").trim());
        druidPlugin.setFilters("stat,wall");
        pl.add(druidPlugin);
        pl.add(new EhCachePlugin());
        ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
        arp.setShowSql(true);
        // 設定資料庫方言
        arp.setDialect(new AnsiSqlDialect());
        pl.add(arp);
        arp.addMapping("userinfo",UserInfo.class);
        arp.addMapping("mission",Mission.class);
    }

先看下Plugins類,這個類很短

final public class Plugins {

    private final List<IPlugin> pluginList = new ArrayList<IPlugin>();

    public Plugins add(IPlugin plugin) {
        if (plugin != null)
            this.pluginList.add(plugin);
        return this;
    }

    public List<IPlugin> getPluginList() {
        return pluginList;
    }
}

Plugins只做了一個功能,把List集合的add方法進行了二次封裝,重點看下IPlugin介面

public interface IPlugin {
    boolean start();
    boolean stop();
}

在jfinalConfig.configPlugin(plugins);方法之後,呼叫了靜態方法
startPlugins();

private static void startPlugins() {
        List<IPlugin> pluginList = plugins.getPluginList();
        if (pluginList == null)
            return ;

        for (IPlugin plugin : pluginList) {
            try {
                // process ActiveRecordPlugin devMode
                if (plugin instanceof com.jfinal.plugin.activerecord.ActiveRecordPlugin) {
                    com.jfinal.plugin.activerecord.ActiveRecordPlugin arp = (com.jfinal.plugin.activerecord.ActiveRecordPlugin)plugin;
                    if (arp.getDevMode() == null)
                        arp.setDevMode(constants.getDevMode());
                }

                if (plugin.start() == false) {
                    String message = "Plugin start error: " + plugin.getClass().getName();
                    log.error(message);
                    throw new RuntimeException(message);
                }
            }
            catch (Exception e) {
                String message = "Plugin start error: " + plugin.getClass().getName() + ". \n" + e.getMessage();
                log.error(message, e);
                throw new RuntimeException(message, e);
            }
        }
    }

這裡遍歷了plugins外掛集合,依次啟動
jfinalConfig.configInterceptor(interceptors); // 新增全域性攔截器