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