博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JFinal 源码导读第三天(2) initActionMapping
阅读量:6100 次
发布时间:2019-06-20

本文共 5908 字,大约阅读时间需要 19 分钟。

  hot3.png

1.接上面文章的内容initActionMapping();该方法我会详细讲解

private void initActionMapping() {		actionMapping = new ActionMapping(Config.getRoutes(), Config.getInterceptors());		actionMapping.buildActionMapping();	}
ActionMapping(Routes routes, Interceptors interceptors) {		this.routes = routes;		this.interceptors = interceptors;	}
2.上面的actionMapping.buildActionMapping();我会详细介绍
private final Map
mapping = new HashMap
();
void buildActionMapping() {		mapping.clear();		Set
excludedMethodName = buildExcludedMethodName(); InterceptorBuilder interceptorBuilder = new InterceptorBuilder(); Interceptor[] defaultInters = interceptors.getInterceptorArray(); interceptorBuilder.addToInterceptorsMap(defaultInters); for (Entry
> entry : routes.getEntrySet()) { Class
controllerClass = entry.getValue(); Interceptor[] controllerInters = interceptorBuilder.buildControllerInterceptors(controllerClass); Method[] methods = controllerClass.getMethods(); for (Method method : methods) { String methodName = method.getName(); if (!excludedMethodName.contains(methodName) && method.getParameterTypes().length == 0) { Interceptor[] methodInters = interceptorBuilder.buildMethodInterceptors(method); Interceptor[] actionInters = interceptorBuilder.buildActionInterceptors(defaultInters, controllerInters, controllerClass, methodInters, method); String controllerKey = entry.getKey(); ActionKey ak = method.getAnnotation(ActionKey.class); if (ak != null) { String actionKey = ak.value().trim(); if ("".equals(actionKey)) throw new IllegalArgumentException(controllerClass.getName() + "." + methodName + "(): The argument of ActionKey can not be blank."); if (!actionKey.startsWith(SLASH)) actionKey = SLASH + actionKey; if (mapping.containsKey(actionKey)) { warnning(actionKey, controllerClass, method); continue; } Action action = new Action(controllerKey, actionKey, controllerClass, method, methodName, actionInters, routes.getViewPath(controllerKey)); mapping.put(actionKey, action); } else if (methodName.equals("index")) { String actionKey = controllerKey; Action action = new Action(controllerKey, actionKey, controllerClass, method, methodName, actionInters, routes.getViewPath(controllerKey)); action = mapping.put(actionKey, action); if (action != null) { warnning(action.getActionKey(), action.getControllerClass(), action.getMethod()); } } else { String actionKey = controllerKey.equals(SLASH) ? SLASH + methodName : controllerKey + SLASH + methodName; if (mapping.containsKey(actionKey)) { warnning(actionKey, controllerClass, method); continue; } Action action = new Action(controllerKey, actionKey, controllerClass, method, methodName, actionInters, routes.getViewPath(controllerKey)); mapping.put(actionKey, action); } } } } // support url = controllerKey + urlParas with "/" of controllerKey Action actoin = mapping.get("/"); if (actoin != null) mapping.put("", actoin); }
3.上面的类比较长,我会结合我的例子来讲

第一个方法buildExcludedMethodName,代码很简单,就是将Controller类中方法无参数的取出来放到excludeMethodName,主要用在下面的判断,我这里简要介绍,就是我们继承Controller的自己类中的方法取出来

private Set
buildExcludedMethodName() { Set
excludedMethodName = new HashSet
(); Method[] methods = Controller.class.getMethods(); for (Method m : methods) { if (m.getParameterTypes().length == 0) excludedMethodName.add(m.getName()); } return excludedMethodName; }

4.上面两段代码非常简单,自己看看就明白啦

InterceptorBuilder interceptorBuilder = new InterceptorBuilder();
Interceptor[] defaultInters = interceptors.getInterceptorArray();
5.interceptorBuilder.addToInterceptorsMap(defaultInters);这个代码我详细讲一下

@SuppressWarnings("unchecked")	void addToInterceptorsMap(Interceptor[] defaultInters) {		for (Interceptor inter : defaultInters)			intersMap.put((Class
)inter.getClass(), inter); }
defaultInters的类就是全局的拦截器,对所有action都是有效的,就是我们下面配置的
/**	 * 配置全局拦截器	 */	public void configInterceptor(Interceptors me) {		me.add(new BlogInterceptor1());		me.add(new BlogInterceptor2());	}
6.routes.getEntrySet()就是我前面配置的, 在我的例子里面就是就是下面map.put中
private final Map
> map = new HashMap
>();public Set
>> getEntrySet() { return map.entrySet(); }
map.put("/", "CommonController.class");
map.put("/blog","BlogController.class")
7.Class<? extends Controller> controllerClass = entry.getValue();
获取到CommonController.class
8.interceptorBuilder.buildControllerInterceptors(controllerClass);这段代码就是相当于
/**	 * Build interceptors of Controller	 */	Interceptor[] buildControllerInterceptors(Class
controllerClass) { Before before = controllerClass.getAnnotation(Before.class); return before != null ? createInterceptors(before) : NULL_INTERCEPTOR_ARRAY; } /** * Create interceptors with Annotation of Before. Singleton version. */ private Interceptor[] createInterceptors(Before beforeAnnotation) { Interceptor[] result = null; @SuppressWarnings("unchecked") Class
[] interceptorClasses = (Class
[]) beforeAnnotation.value(); if (interceptorClasses != null && interceptorClasses.length > 0) { result = new Interceptor[interceptorClasses.length]; for (int i=0; i
相当于获取类上面的Before的注解,并且放入到 intersMap中
controllerInters相当于自定义Controller上面的注解信息,BlogInterceptor3.class和BlogInterceptor4.class
private Map<Class<Interceptor>, Interceptor> intersMap = new HashMap<Class<Interceptor>, Interceptor>();
/** * BlogController * 注意:在实际项目中业务与sql需要写在Model中,此demo仅为示意,故将sql写在了Controller中 */@Before({BlogInterceptor3.class,BlogInterceptor4.class})public class BlogController extends Controller {

今天就介绍到这里,明天继续接下来的代码,我会详细的讲,如果谁有问题,请留言,我会及时回复

转载于:https://my.oschina.net/skyim/blog/138249

你可能感兴趣的文章
关闭电脑自动更新(服务与组策略)
查看>>
MyBatis中的resultType和resultMap
查看>>
体验应用程序在Mac/iPhone/iPad之间的Handoff
查看>>
ThinkPHP框架介绍
查看>>
关于对html中文档流的理解
查看>>
Linux组件封装(六)——定时器的简单封装
查看>>
拓展随记
查看>>
[iOS]SourceTree+oschina实现代码远程托管
查看>>
js字符串转数组
查看>>
__attribute__机制
查看>>
jquery打造一个会自动播放样子也很经典的选项卡tab
查看>>
JAVA的类和对象
查看>>
css3 transition
查看>>
ural(Timus) 1333. Genie Bomber 2
查看>>
fiddler学习笔记--基本介绍
查看>>
开发者眼中最好的 22 款 GUI 测试工具
查看>>
zz iOS Core Animation 简明系列教程
查看>>
oracle免安装客户端设置
查看>>
c/c++内存使用原则
查看>>
关于程序员的发展方向
查看>>