为了账号安全,请及时绑定邮箱和手机立即绑定

源码有毒:Jfinal源码解析(二)

标签:
Java
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); // 添加全局拦截器

源码有毒:Jfinal源码解析(一)

点击查看更多内容
4人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
JAVA开发工程师
手记
粉丝
8548
获赞与收藏
6550

关注作者,订阅最新文章

阅读免费教程

感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消